Skip to content

Commit 7b2183c

Browse files
authored
Create README.md
1 parent 5d549f2 commit 7b2183c

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

lesson_04/chelseaogbonnia/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.

0 commit comments

Comments
 (0)