Skip to content

Commit 16a45ec

Browse files
feat: adds Jaizel's code samples (#150)
1 parent b69b07d commit 16a45ec

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

lesson_04/jaizelc/Prime_checker.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def is_prime(number):
2+
3+
if number < 2:
4+
return False
5+
6+
for i in range(2, number):
7+
8+
if number % i == 0:
9+
return False
10+
11+
return True
12+
13+
14+
print("Is 2 prime?", is_prime(2))
15+
print("Is 4 prime?", is_prime(4))
16+
print("Is 7 prime?", is_prime(7))

lesson_04/jaizelc/Primechecker.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function isPrime(number) {
2+
if (number < 2) {
3+
return false;
4+
}
5+
if (number === 2) {
6+
return true;
7+
}
8+
if (number % 2 === 0) {
9+
return false;
10+
}
11+
12+
for (let i = 3; i <= Math.sqrt(number); i += 2) {
13+
if (number % i === 0) {
14+
return false;
15+
}
16+
}
17+
18+
return true;
19+
}
20+
21+
console.log("Is 2 prime?", isPrime(2));
22+
console.log("Is 4 prime?", isPrime(4));
23+
console.log("Is 7 prime?", isPrime(7));
24+
console.log("Is 15 prime?", isPrime(15));
25+
console.log("Is 17 prime?", isPrime(17));

lesson_04/jaizelc/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
### How to Run Each Implementation
2+
## Python:
3+
4+
- **Open terminal in VS Code or command prompt**
5+
- **Run the command: python prime_checker.py**
6+
- **The results will be displayed in the terminal**
7+
8+
## JavaScript:
9+
10+
- **Open terminal in VS Code or command prompt**
11+
- **Make sure Node.js is installed on your system**
12+
- **Run the command: node prime_checker.js**
13+
- **The results will be displayed in the terminal**
14+
15+
## Explanation
16+
The Python implementation uses a function named is_prime that takes a single argument number. It returns True if the number is prime, otherwise it returns False. The function first checks if the number is less than 2, then tests for divisibility by all numbers from 2 up to the number minus 1 using a simple brute-force approach.
17+
The JavaScript implementation uses a function named isPrime that also takes a single argument number. It returns true if the number is prime and false otherwise. The JavaScript version uses an optimized approach by only checking divisors up to the square root of the number, making it more efficient for larger numbers.
18+
19+
## Differences
20+
Equality Comparison:
21+
22+
- **Python uses == for equality comparison, while JavaScript uses === for strict equality to avoid type coercion issues.**
23+
24+
Type Handling:
25+
26+
- **JavaScript has type coercion, which can sometimes lead to unexpected results if the input is not properly handled. In contrast, Python is more strict with types and will raise errors for incompatible operations.**
27+
28+
Algorithm Efficiency:
29+
30+
- **The Python implementation uses a brute-force approach, checking all numbers from 2 to number-1 for divisibility.**
31+
- **The JavaScript implementation is more optimized, only checking divisors up to the square root of the number, making it faster for larger inputs.**

0 commit comments

Comments
 (0)