Skip to content

Commit e0f8d42

Browse files
feat(prime-checker): add Python is_prime function to lesson_04
1 parent e5f3d0c commit e0f8d42

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

lesson_04/jonee_prime.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import math
2+
3+
4+
def is_prime(number):
5+
"""
6+
Determines if a given number is a prime number.
7+
8+
A prime number is a natural number greater than 1 that has no positive divisors
9+
other than 1 and itself.
10+
11+
Args:
12+
number (int): The integer to check.
13+
14+
Returns:
15+
bool: True if the number is prime, False otherwise.
16+
"""
17+
if number <= 1:
18+
return False # Numbers less than or equal to 1 are not prime
19+
if number == 2:
20+
return True # 2 is the only even prime number
21+
if number % 2 == 0:
22+
return False # Other even numbers are not prime
23+
24+
# Check for divisors from 3 up to the square root of the number,
25+
# incrementing by 2 to only check odd numbers.
26+
for i in range(3, int(math.sqrt(number)) + 1, 2):
27+
if number % i == 0:
28+
return False # Found a divisor, so it's not prime
29+
return True # No divisors found, it's prime
30+
31+
# Example usage:
32+
# You can uncomment these lines and run the file to see the output
33+
# print(f"Is 7 prime? {is_prime(7)}")
34+
# print(f"Is 10 prime? {is_prime(10)}")
35+
# print(f"Is 2 prime? {is_prime(2)}")
36+
# print(f"Is 1 prime? {is_prime(1)}")
37+
# print(f"Is 13 prime? {is_prime(13)}")
38+
# print(f"Is 49 prime? {is_prime(49)}")

0 commit comments

Comments
 (0)