Skip to content

Commit e857016

Browse files
authored
feat: adds sample code for Chelsea (#181)
1 parent 77b54a8 commit e857016

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

lesson_04/chelseaogbonnia/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Prime Number Finder (Python and JavaScript) #
2+
3+
This project demonstrates how to find prime numbers between 1 and 100 using two programming languages: Python and JavaScript. Each function uses a for loop to check whether a number is prime, and both include inline comments to explain the code step by step. Additionally, there are detailed explanations after each function.
4+
5+
## Prime Number Finder in Python ##
6+
```
7+
# Function to find prime numbers between 1 and 100 in Python
8+
def find_prime_num_py():
9+
10+
# Loop through numbers from 2 to 100
11+
for num in range(2, 101):
12+
is_prime = True # Assume the number is prime
13+
14+
# Check if num is divisible by any number between 2 and its square root
15+
for i in range(2, int(num**0.5) + 1):
16+
if num % i == 0:
17+
is_prime = False # If divisible, not a prime number
18+
break # Exit the loop early
19+
20+
# If is_prime is still true, num is a prime number
21+
if is_prime:
22+
print(num) # Output the prime number
23+
24+
# Call the Python function
25+
find_prime_num_py()
26+
```
27+
### Python Explanation: ###
28+
The Python function, find_prime_num_py, uses a for loop to iterate through the numbers between 2 and 100. The is_prime variable is set to True for each number. Inside the inner loop, we check if the number is divisible by any integer between 2 and the square root of the number. If the number is divisible, it is marked as not prime, and we break out of the inner loop to stop further unnecessary checks. The square root limit ensures better performance. When the number is confirmed to be prime, it is printed to the console.
29+
30+
31+
## Prime Number Finder in JavaScript ##
32+
```
33+
// Function to find prime numbers between 1 and 100 in JavaScript
34+
function findPrimeNumberJS() {
35+
36+
// Loop through numbers from 2 to 100
37+
for (let num = 2; num <= 100; num++) {
38+
let isPrime = true; // Assume the number is prime
39+
40+
// Check if num is divisible by any number between 2 and its square root
41+
for (let i = 2; i <= Math.sqrt(num); i++) {
42+
if (num % i === 0) {
43+
isPrime = false; // If divisible, not a prime number
44+
break; // Exit the loop early
45+
}
46+
}
47+
48+
// If isPrime is still true, num is a prime number
49+
if (isPrime) {
50+
console.log(num); // Output the prime number
51+
}
52+
}
53+
}
54+
55+
// Call the JavaScript function
56+
findPrimeNumberJS();
57+
```
58+
59+
### JavaScript Explanation: ###
60+
In this JavaScript function, findPrimeNumberJS, we use a for loop to iterate over the numbers from 2 to 100. The isPrime variable initially assumes that each number is prime. For each number num, we check if it is divisible by any number from 2 to the square root of num. If the number is divisible, it is marked as not prime by setting isPrime to false, and we break out of the inner loop to save time. If the number remains prime after the check, it is printed to the console. The square root check ensures we minimize the number of iterations for efficiency.
61+
62+
## Similarities and Differences Between Python and JavaScript Prime Number Functions ##
63+
64+
### Similarities
65+
66+
* Logic (Using Square Roots to Check Primes): Both functions in Python and JavaScript use the same fundamental logic to determine if a number is prime. By checking divisors only up to the square root of the number, we can efficiently reduce the number of checks. This optimization works in both languages because if a number has a divisor greater than its square root, it must also have a corresponding smaller divisor, which would have been checked already.
67+
68+
* Looping Structure: Both functions use a for loop to iterate over potential divisors, starting from 2. While the exact syntax differs, the purpose is the same—to test whether the number is divisible by any number within this range. If no divisor is found, the number is prime.
69+
70+
71+
### Differences:
72+
73+
* Syntax: Python uses range() to generate numbers in a loop, while JavaScript directly declares the loop variable and sets its conditions. Python also uses indentation to define code blocks, while JavaScript uses curly braces {}.
74+
75+
* Case Sensitivity: Python commonly uses snake_case for function and variable names (e.g., is_prime), while JavaScript typically uses camelCase (e.g., isPrime).
76+
77+
* Variable Declarations: In Python, variables are declared by simply assigning a value to them (e.g., num = 5). In JavaScript, variables must be explicitly declared using let, const, or var (e.g., let num = 5).

0 commit comments

Comments
 (0)