Check For Prime
# Check if a Number is Prime
Pattern: Prime numbers
Idea: factors occur in pairs (x,y) -> x*y = n where
π» Code
def is_prime(n):
if n <= 1:
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
Time complexity - Aux. Space complexity - O(1) Note : Further optimized soln. below
A prime number is a positive integer greater than 1 that has exactly two positive divisors:
-
1 -
Itself
Examples:
2, 3, 5, 7, 11, 13...
Non-prime:
1 (only one divisor)
4 (1, 2, 4)
12 (1, 2, 3, 4, 6, 12)
Naive Approach
Idea
Check if any number from 2 to n-1 divides n.
If yes β Not Prime.
Otherwise β Prime.
Python
def is_prime(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
Complexity
-
Time:
-
Space:
Optimized Approach (Square Root)
Key Observation
Factors always occur in pairs.
Example:
36
1 Γ 36
2 Γ 18
3 Γ 12
4 Γ 9
6 Γ 6
Notice:
-
One factor is less than or equal to .
-
The other is greater than or equal to .
If there were no factor β€ , then there couldnβt be a corresponding larger factor either.
Therefore, it is sufficient to check divisibility only up to .
Why?
Assume n is composite.
Then
n = a Γ b
Suppose both factors were greater than .
Then
Multiplying,
which is impossible because
ab = n
Hence at least one factor must be β€ .
Python
def is_prime(n):
if n <= 1:
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
Interview Tip: ==Prefer
i * i <= noveri <= sqrt(n)to avoid repeated square root calculations and floating-point arithmetic.==
Dry Run
Check n = 29
β29 β 5.38
Check only
2
3
4
5
None divide 29.
Therefore,
29 is Prime
Check n = 35
2 β
3 β
4 β
5 β
Stop immediately.
35 is Not Prime
Even Better Optimization (6k Β± 1)
Observation
Every integer can be written as one of:
6k
6k + 1
6k + 2
6k + 3
6k + 4
6k + 5
Among these,
-
6kβ divisible by 6 -
6k + 2β even -
6k + 3β divisible by 3 -
6k + 4β even
So every prime greater than 3 must be of the form
6k Β± 1
Important: This is a necessary condition, not a sufficient one.
Example:
25 = 6Γ4 + 1but 25 is not prime.
So after checking 2 and 3, we only test numbers:
5, 7, 11, 13, 17, 19, ...
Python
def is_prime(n):
if n <= 1:
return False
# Handle small primes separately
if n == 2 or n == 3 or n == 5:
return True
# Eliminate obvious composites
if n % 2 == 0 or n % 3 == 0 or n % 5 == 0:
return False
i = 7
while i * i <= n:
if n % i == 0: # 6k + 1
return False
i += 4 # Move to 6k + 5
if i * i <= n and n % i == 0:
return False
i += 2 # Move to next 6(k+1) + 1
return True
Complexity
| Approach | Time | Space | |
|---|---|---|---|
Check 2...n-1 | |||
| Check up to | |||
| 6k Β± 1 Optimization | (fewer iterations) |
The improvement is only in the constant factor
Suppose youβre checking up to .
Regular method
Checks
2,3,4,5,6,7,...,100
β 99 numbers.
6k Β± 1
Checks
5,7,11,13,17,19,...
Only numbers not divisible by 2 or 3.
About one-third of the candidates remain.
So instead of roughly
100 checks
you perform roughly
33 checks
Thatβs about a 3Γ speedup, but the algorithm is still proportional to .
Interview Takeaways
-
A prime number has exactly two positive divisors.
-
0and1are not prime. -
Checking up to
n-1is unnecessary. -
Factors always occur in pairs.
-
It is enough to check divisors up to .
-
Use
i * i <= ninstead ofsqrt(n)in code. -
The 6k Β± 1 optimization reduces constant factors but does not change the asymptotic complexity.
-
For checking many numbers in a range, use the Sieve of Eratosthenes instead of checking each number individually.