Skip to content

Commit 40dd712

Browse files
authored
feat: adds Pablo's Java and Python Prime number finder with explanation differences and similarities (#174)
1 parent e857016 commit 40dd712

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

lesson_04/pablolimonparedes/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## Java Implementation
2+
```Java
3+
public class Main {
4+
public static void main(String[]args){
5+
int num = 19;
6+
boolean isPrime = true;
7+
for (int i = 2; i < num; i++){
8+
if (num % i == 0){
9+
isPrime = false;
10+
break;
11+
}
12+
}
13+
if (isPrime){
14+
System.out.println(num + " is a Prime number.");
15+
} else {
16+
System.out.println(num + " is not a Prime number.");
17+
}
18+
}
19+
}
20+
//Had help from Chat GPT to help me understand how to make one and break it down step by step//
21+
```
22+
## Python Implementation
23+
``` Python
24+
def is_prime(num):
25+
if num <= 1:
26+
return False
27+
28+
for i in range(2, num):
29+
if num % i == 0:
30+
return False
31+
32+
return True
33+
num = 19
34+
if is_prime(num):
35+
print(f"{num} is a Prime number.")
36+
else:
37+
print(f"{num} is Not a Prime number.")
38+
39+
## I Had Chat GPT convert this from the Java equation ##
40+
```
41+
## Explanation
42+
Here i have the equations to find prime numbers in both Java and Python, however it is hard coded which means that if you wanted to figure out that another number was a prime number you would need to change the value in the variable num.
43+
44+
## Differences
45+
- Java uses curly braces with `else`, while python uses a colon with `else` to define the blocks of code.
46+
- Java has to use `public static boolean` to define the method, as in this Python equation does not use any class to define its method since it does not have one in this case, but instead uses a function with the keyword `def` to declare it.
47+
- Java and Python both use a `for` loop however Python uses `range` to create the sequence of numbers from 2 till num and in the Java code `for (int i = 2; i < num; i++)` you have to initialize, then have the condition, and lastly increment.
48+
## Similarities
49+
- Both Codes print out whether or not the number is prime, if you would like to find another number you just need to change the value from the variable num.
50+
- They both use some type of print to output what the solution of your value is to know whether or not your number that is inputted is prime.

0 commit comments

Comments
 (0)