Skip to content

Commit f297fd4

Browse files
authored
feat: adds prime number implementation for Zion (#150)
* provided impletemtations to determine prime number using javasript and java. * Made updates to lesson_04 * I provided a precise explanation in the differences section.
1 parent 9724b0b commit f297fd4

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

lesson_04/zionbuchanan/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
## JavaScript Implementation
2+
3+
```javascript
4+
// Function to check if a number is prime
5+
function isPrime(number) {
6+
// A prime number must be greater than 1
7+
if (number <= 1) {
8+
return false;
9+
}
10+
11+
// Check if the number has any divisors other than 1 and itself
12+
for (let i = 2; i <= Math.sqrt(number); i++) {
13+
// If the number is divisible by i, it's not prime
14+
if (number % i === 0) {
15+
return false;
16+
}
17+
}
18+
19+
// If no divisors were found, the number is prime
20+
return true;
21+
}
22+
23+
// Example usage
24+
let num = 77;
25+
if (isPrime(num)) {
26+
console.log(num + " is a prime number.");
27+
} else {
28+
console.log(num + " is not a prime number.");
29+
}
30+
```
31+
## Java Implementation
32+
33+
```java
34+
// Prime Number Checker in Java
35+
public class PrimeChecker {
36+
37+
// Function to check if a number is prime
38+
public static boolean isPrime(int number) {
39+
// A prime number must be greater than 1
40+
if (number <= 1) {
41+
return false;
42+
}
43+
44+
// Check if the number has any divisors other than 1 and itself
45+
for (int i = 2; i <= Math.sqrt(number); i++) {
46+
// If the number is divisible by i, it's not prime
47+
if (number % i == 0) {
48+
return false;
49+
}
50+
}
51+
52+
// If no divisors were found, the number is prime
53+
return true;
54+
}
55+
56+
public static void main(String[] args) {
57+
// Example usage
58+
int num = 77;
59+
if (isPrime(num)) {
60+
System.out.println(num + " is a prime number.");
61+
} else {
62+
System.out.println(num + " is not a prime number.");
63+
}
64+
}
65+
}
66+
```
67+
## Explanation
68+
For JavaScript, I created a fuction using chatgpt that would determine whether a number is prime or not. If a number is less than or equal to 1, then the output of the function would determine the number to be false. This is due to the fact that any number less than or equal to 1 is not a prime number. Any number that is greater than 1 would simply return the output of the function to be true or false, depending on if it is or isn't a prime number. I then included a variable that would mathematically verify if any given number has any divisors others than the number 1 and itself. If the number is divisible by i, then it's not prime. It would only be prime if there were no divisors found.
69+
70+
For Java, it was a similar process, but with more structure in the data types. I also used chat gpt to help me understand this process as well.
71+
## Differences
72+
In regards to the syntax and structure, Java was more strict with its rules while JavaScript was more loose. In other words, Java had a 11pm curfew while JavaScript could go out and come back at whatever time it pleases. The data type in Java required explicit commands whereas JavaScript had more leniency towards its data types. In other words, JavaScript functions are flexible, with no need for type declarations or class associations. Java methods require explicit return and parameter types and must reside within a class.

0 commit comments

Comments
 (0)