Skip to content

Commit d7b5c6b

Browse files
Tezz03“Tezz03”anthonydmays
authored
feat: adds Montez's code samples (#163)
* chore: compare and contrast lesson_04 * chore: forgot to add the README file * chore: moves the files Signed-off-by: Anthony D. Mays <[email protected]> * chore: moves files to the correct location Signed-off-by: Anthony D. Mays <[email protected]> * chore: adjusting formatting Signed-off-by: Anthony D. Mays <[email protected]> --------- Signed-off-by: Anthony D. Mays <[email protected]> Co-authored-by: “Tezz03” <“[email protected]”> Co-authored-by: Anthony D. Mays <[email protected]>
1 parent 03d0a0e commit d7b5c6b

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

lesson_04/montez_L4/PrimeChecker.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.Scanner;
2+
3+
public class PrimeChecker {
4+
5+
public static void main(String[] args) {
6+
Scanner scanner = new Scanner(System.in);
7+
System.out.print("Enter a number: ");
8+
int number = scanner.nextInt();
9+
10+
if (isPrime(number)) {
11+
System.out.println(number + " is a prime number");
12+
} else {
13+
System.out.println(number + " is not a prime number");
14+
}
15+
}
16+
17+
public static boolean isPrime(int number) {
18+
// Numbers less than or equal to 1 are not prime
19+
if (number <= 1) {
20+
return false;
21+
}
22+
23+
// Check from 2 up to the square root of the number
24+
for (int i = 2; i <= Math.sqrt(number); i++) {
25+
// If the number is divisible by any number in the range, it's not prime
26+
if (number % i == 0) {
27+
return false;
28+
}
29+
}
30+
31+
// If no divisors were found, the number is prime
32+
return true;
33+
}
34+
}
35+

lesson_04/montez_L4/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Lesson 04:
2+
3+
## Python code
4+
5+
```python
6+
def is_prime(n):
7+
"""Check if a number is prime."""
8+
if n < 2:
9+
return False
10+
if n in (2, 3):
11+
return True
12+
if n % 2 == 0 or n % 3 == 0:
13+
return False
14+
15+
# Check divisibility using 6k ± 1 rule
16+
i = 5
17+
while i * i <= n:
18+
if n % i == 0 or n % (i + 2) == 0:
19+
return False
20+
i += 6
21+
return True
22+
23+
def main():
24+
try:
25+
num = int(input("Enter a number: "))
26+
if is_prime(num):
27+
print(f"{num} is a prime number.")
28+
else:
29+
print(f"{num} is not a prime number.")
30+
except ValueError:
31+
print("Invalid input! Please enter an integer.")
32+
33+
Run the program
34+
if __name__ == "__main__":
35+
main()
36+
```
37+
38+
## Java Code
39+
40+
```java
41+
import java.util.Scanner;
42+
43+
public class PrimeChecker {
44+
45+
public static void main(String[] args) {
46+
Scanner scanner = new Scanner(System.in);
47+
System.out.print("Enter a number: ");
48+
int number = scanner.nextInt();
49+
50+
if (isPrime(number)) {
51+
System.out.println(number + " is a prime number");
52+
} else {
53+
System.out.println(number + " is not a prime number");
54+
}
55+
}
56+
57+
public static boolean isPrime(int number) {
58+
// Numbers less than or equal to 1 are not prime
59+
if (number <= 1) {
60+
return false;
61+
}
62+
63+
// Check from 2 up to the square root of the number
64+
for (int i = 2; i <= Math.sqrt(number); i++) {
65+
// If the number is divisible by any number in the range, it's not prime
66+
if (number % i == 0) {
67+
return false;
68+
}
69+
}
70+
71+
// If no divisors were found, the number is prime
72+
return true;
73+
}
74+
}
75+
```
76+
77+
## Compare and Contrast Java and Python
78+
79+
In this assignment I am comparing and contrasting python’s code to Java’s code. These codes are about selecting prime numbers and the differences I see between them. The first thing I notice with these codes is Java’s code is three times bigger than the code in Python. With that being said it seems like Java needs more written code in it to achieve the same goal Python has. Another thing I have noticed is that Java you have to import java.util.Scanner; this just means that the scanner class is available for our program. Java also has public static void main(String[] args) which is the entry point for the java program. One thing that I noticed was that Python did not have any else statements. In python it seems as though you don’t have to declare n as a number while in Java you have to. Some similarities that Java and Python have in common are that they both use return, int, if, and for statements to complete the code.

lesson_04/montez_L4/index.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def is_prime(n):
2+
"""Check if a number is prime."""
3+
if n < 2:
4+
return False
5+
if n in (2, 3):
6+
return True
7+
if n % 2 == 0 or n % 3 == 0:
8+
return False
9+
10+
# Check divisibility using 6k ± 1 rule
11+
i = 5
12+
while i * i <= n:
13+
if n % i == 0 or n % (i + 2) == 0:
14+
return False
15+
i += 6
16+
return True
17+
18+
19+
def main():
20+
try:
21+
num = int(input("Enter a number: "))
22+
if is_prime(num):
23+
print(f"{num} is a prime number.")
24+
else:
25+
print(f"{num} is not a prime number.")
26+
except ValueError:
27+
print("Invalid input! Please enter an integer.")
28+
29+

0 commit comments

Comments
 (0)