Skip to content

Commit 4f9a867

Browse files
authored
feat: adds Olivia's code samples (code-differently#175)
1 parent c8a825a commit 4f9a867

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

lesson_04/oliviajames/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
def is_prime(n):
2+
if n < 2:
3+
return False
4+
if n in (2, 3):
5+
return True
6+
if n % 2 == 0 or n % 3 == 0:
7+
return False
8+
9+
i = 5
10+
while i * i <= n:
11+
if n % i == 0 or n % (i + 2) == 0:
12+
return False
13+
i += 6
14+
return True
15+
16+
# Example usage
17+
num = int(input("Enter a number: "))
18+
print(f"{num} is prime: {is_prime(num)}")
19+
20+
21+
import java.util.Scanner;
22+
23+
public class PrimeChecker {
24+
public static boolean isPrime(int n) {
25+
if (n < 2) return false;
26+
if (n == 2 || n == 3) return true;
27+
if (n % 2 == 0 || n % 3 == 0) return false;
28+
29+
for (int i = 5; i * i <= n; i += 6) {
30+
if (n % i == 0 || n % (i + 2) == 0) return false;
31+
}
32+
return true;
33+
}
34+
35+
public static void main(String[] args) {
36+
Scanner scanner = new Scanner(System.in);
37+
System.out.print("Enter a number: ");
38+
int num = scanner.nextInt();
39+
scanner.close();
40+
41+
System.out.println(num + " is prime: " + isPrime(num));
42+
}
43+
}
44+
45+
46+
# Explanation
47+
After examining both languages I noticed that Python uses an is_prime (num) function that is used to dictate whether the number being input is prime or not. It uses specific clauses including numbers less than 1, even numbers greater than 2 , and numbers from 3 to the square root of said number for all statements to print false, otherwise it will print true if the number is equal to 2 as it is the smallest prime number and all others outside of said classes.
48+
49+
In Java it uses a class function primechecker and an isPrime(int num) to determine if a number is prime. Using if statements that will return false when numbers are less than 1, numbers that are even or greater than 2, and then uses a loop to divide from 2 to the square root of the number. If no divisor is found it will otherwise print true and if the number is equal to 2.
50+
51+
52+
53+
54+
# Differences
55+
In python, functions are defined using the def keyword, whereas in Java, public class is used as a keyword.
56+
Java uses “System.out.print” and python uses “print”
57+
Java uses a public static boolean while python uses def is_prime(num).
58+
59+
60+

0 commit comments

Comments
 (0)