Here is a Python function to check if a number is prime, along with a detailed explanation of how it works.
We'll start with the most common and efficient version and then show a simpler, more intuitive version for learning.
This version includes optimizations to make it run faster, especially for large numbers.
`
python
import math
def is_prime(num):
"""
Checks if a number is a prime number.
A prime number is a natural number greater than 1 that has no positive
divisors other than 1 and itself.
"""
# 1. Handle edge cases
# Prime numbers must be greater than 1.
if num <= 1:
return False
# 2 is the only even prime number.
if num == 2:
return True
# All other even numbers are not prime.
if num % 2 == 0:
return False
# 2. Check for odd divisors from 3 up to the square root of the number.
# We can skip even numbers in our loop.
# We only need to check up to the square root because if a number n has a
# divisor larger than its square root, it must also have a divisor smaller
# than its square root.
for i in range(3, int(math.sqrt(num)) + 1, 2):
if num % i == 0:
# If we find a divisor, the number is not prime.
return False
# 3. If no divisors were found, the number is prime.
return True
print(f"Is 2 a prime number? {is_prime(2)}")
print(f"Is 3 a prime number? {is_prime(3)}")
print(f"Is 4 a prime number? {is_prime(4)}")
print(f"Is 29 a prime number? {is_prime(29)}")
print(f"Is 99 a prime number? {is_prime(99)}")
print(f"Is 1 a prime number? {is_prime(1)}")
print(f"Is 0 a prime number? {is_prime(0)}")
`
Is 2 a prime number? True
Is 3 a prime number? True
Is 4 a prime number? False
Is 29 a prime number? True
Is 99 a prime number? False
Is 1 a prime number? False
Is 0 a prime number? False
Handle Edge Cases First:
The definition of a prime number is a natural number greater than 1. So, any number less than or equal to 1 (num <= 1
) is immediately disqualified. We return False
.
The number 2 is the only even prime number. We handle it as a special case to simplify the rest of the logic.
* All other even numbers (like 4, 6, 8) are divisible by 2, so they cannot be prime. We check for this with num % 2 == 0
and return False
.
The Loop and The Square Root Optimization:
If a number num
has a divisor d
, then `num = d c. If
d is larger than the square root of
num, then
c must be smaller. This means we only need to search for divisors up to
sqrt(num). If we don't find one, we know the number is prime. This dramatically reduces the number of checks needed for large numbers.
*
int(math.sqrt(num)) + 1: We calculate the square root, convert it to an integer, and add 1 to make sure the loop's upper bound is inclusive.
*
range(3, ..., 2): Since we've already eliminated all even numbers, we can start our search at 3 and increment the divisor
i by 2 in each step (
3, 5, 7, ...`). This halves the number of loop iterations.
Check for Divisibility:
Inside the loop, if num % i == 0:
checks if i
divides num
without a remainder.
If it does, we have found a factor, which means num
is not prime. We can immediately return False
and exit the function.
If the Loop Completes:
* If the for
loop finishes without finding any divisors, it means the number has no factors other than 1 and itself. Therefore, it is a prime number, and the function returns True
.
If you are new to programming, this version is more straightforward, although less efficient for large numbers. It checks every number from 2 up to num - 1
.
`
python
def is_prime_simple(num):
"""
A simple, but less efficient, way to check for a prime number.
"""
# Prime numbers must be greater than 1.
if num <= 1:
return False
# Check for any divisors from 2 up to the number just before itself.
for i in range(2, num):
if num % i == 0:
# If we find a divisor, it's not prime.
return False
# If the loop completes, it's a prime number.
return True
print(f"Is 17 a prime number? {is_prime_simple(17)}") # True
print(f"Is 18 a prime number? {is_prime_simple(18)}") # False
`