Is It Prime?
A prime is a whole number greater than 1 whose only divisors are 1 and itself.
Write is_prime(n) that returns True or False. The tests include numbers with nine digits, so the first version you write may be too slow.
Understand the problem
Check the definition on small numbers.
Is 1 a prime?
- Yes
- No
A prime must be greater than 1. The number 1 has only one divisor, itself, and is not prime. Zero and negative numbers are not prime either.
Is 2 a prime?
- Yes
- No
2 has exactly two divisors, 1 and 2. It is also the only even prime, which is why special cases for 2 show up in prime code.
Write the brute force
Write the simplest version that is correct, even if it is slow. Then you have something to test against and something to improve.
def is_prime(n):
# Write the simple version here. It only has to be correct, not fast.
pass
Hint 1: a question to ask yourself
What must be true for a number that is not prime?
Hint 2: the shape of the answer
A number below 2 is not prime. Otherwise try every i from 2 to n - 1: if n % i == 0 for any of them, n is not prime.
Find the bottleneck
Think about how many divisors you really need to try.
If n = 100 has a divisor larger than 10, it has a partner divisor smaller than 10 (because the two multiply to 100). So what is the largest i you must test for n = 100?
Divisors come in pairs (i, n / i). One of the pair is always at most the square root. If you find no divisor up to the square root (10 for 100), there is none above it either.
For n = 1,000,000,007, about how many divisors does the brute force test, and how many do you need to test with the square root idea?
- One billion versus about 31,600
- One million versus 1,000
- The same number
The square root of 1,000,000,007 is about 31,623. The brute force makes a billion checks; the square root version makes about thirty thousand. That is a difference of a factor of 30,000.
Make it faster
Stop as soon as the divisor passes the square root.
def is_prime(n):
# Write the faster version here.
pass
Hint 1: a question to ask yourself
Where can the loop stop?
Hint 2: the pattern
Loop while i * i <= n, or use range(2, math.isqrt(n) + 1). You can also test 2 on its own and then only odd numbers.
Hint 3: pseudocode
if n < 2: return False
if n is even: return n == 2
for i in 3, 5, 7, ... while i * i <= n:
if n % i == 0: return False
return True
Reflect
Brute force: O(n)
def is_prime(n):
if n < 2:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
Improved: O(square root of n)
import math
def is_prime(n):
if n < 2:
return False
if n % 2 == 0:
return n == 2
for i in range(3, math.isqrt(n) + 1, 2):
if n % i == 0:
return False
return True
What this tests: loops with early exit, range with a step, math.isqrt, and noticing that work can be cut by a mathematical fact.
The idea: divisors come in pairs, so you only need to search up to the square root. That turns a billion checks into thirty thousand. Skipping even numbers halves it again.
Cost:
| Version | Time |
|---|---|
| Try every number below n | O(n) |
| Stop at the square root | O(square root of n) |
Use isqrt, not n ** 0.5: floating point can be off by one for very large n; math.isqrt is exact for integers.
What an interviewer might ask next: You must test many numbers up to a million: what is a faster way than testing each one? (The sieve of Eratosthenes.)
VISIBLE = 3
CASES = [
((17,), True, 'a small prime'),
((100,), False, 'a small composite'),
((2,), True, 'the only even prime'),
((0,), False, 'zero'),
((1,), False, 'one'),
((-7,), False, 'a negative number'),
((25,), False, 'the square of a prime'),
((97,), True, 'a two-digit prime'),
((7919,), True, 'the 1000th prime'),
((91,), False, 'seven times thirteen'),
((3,), True, 'three'),
((4,), False, 'four'),
]
def REFERENCE(n):
if n < 2:
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
LIMIT = 2.0
def PERF():
return [('a prime near a hundred million', (100000007,)), ('a prime with ten digits', (2147483647,)), ('the square of a big prime', (999983 * 999983,))]