Sub topic 6
Why You Only Need to Check Up to the Square Root
This chapter assumes you have read the trial division chapter, can read Python loops, and know introductory Big-O notation. Trial division stopped at the condition d * d <= n. Here you will see why that bound is correct, why it is the tightest simple bound, and how to write it without floating-point bugs.
Divisors come in pairs
Each divisor d of n has a partner n // d, and the two multiply to n. If one member of a pair is small, the other is large. The pairs meet in the middle at the square root of n.
def divisor_pairs(n):
pairs = []
d = 1
while d * d <= n:
if n % d == 0:
pairs.append((d, n // d))
d += 1
return pairs
print(divisor_pairs(36)) # [(1, 36), (2, 18), (3, 12), (4, 9), (6, 6)]
print(divisor_pairs(37)) # [(1, 37)]The loop never goes past 6 for n = 36, and it still finds every divisor, because each large divisor shows up as the partner of a small one. For 37 the only pair is (1, 37), so 37 is prime.
The argument
Suppose n is composite, so n = a * b with 1 < a <= b < n. If both a and b were greater than √n, then a * b > √n * √n = n, which contradicts a * b = n. So the smaller factor a satisfies a <= √n.
Turned around: if no d with 2 <= d <= √n divides n, then n has no factorization into two factors larger than 1, so n is prime.
The smallest factor found this way is always prime. If it were composite, one of its own smaller factors would also divide n and would have been found first.
How much work this saves
| n | Divisors tried up to n − 1 | Divisors tried up to √n |
|---|---|---|
| 10,000 | about 10,000 | 100 |
| 1,000,000 | about 1,000,000 | 1,000 |
| 10^12 | about 10^12 | 10^6 |
Testing a prime costs O(√n) instead of O(n). For a 12-digit prime, that is the difference between a millisecond and hours.
Writing the bound correctly
The two common mistakes are using < where you need <=, and computing √n with floating point.
With d * d < n, the loop skips d = 7 when n = 49, so 49 is reported as prime. The boundary case is a perfect square of a prime, so the condition must include equality.
Floating point fails for large integers. A Python float has 53 bits of mantissa, so integers above 2^53 cannot all be stored exactly, and math.sqrt rounds the result.
import math
n = 10**18 - 1
print(int(math.sqrt(n)), math.isqrt(n)) # 1000000000 999999999The float version is one too large here. That error only costs one extra iteration, but in other cases rounding goes the other way and the loop skips the last divisor it needs. Use integer comparisons instead.
Where this bound appears again
The same reasoning shapes the sieve of Eratosthenes in the next chapters. When you sieve up to n, you only need to cross out multiples of primes p with p * p <= n. Every composite number up to n has a prime factor no larger than √n, so it has already been crossed out by the time p passes that limit. The segmented sieve uses this fact too: it needs only the base primes up to √hi.
Exercise
A trial-division test uses the condition d d < n instead of d d <= n. Name the smallest input it misclassifies, and describe every input it gets wrong.
Show answer
The smallest is 4. The loop never runs because 2 2 < 4 is false, so 4 is reported as prime. The test gets wrong exactly the squares of primes (4, 9, 25, 49, ...). Any other composite n has a factor pair a < b, so a a < n and the loop finds a.
Exercise
What is the largest divisor d that trial division tries when n = 101, and how many candidate divisors does it test in total?
Show answer
math.isqrt(101) is 10 because 10 * 10 = 100 <= 101 < 121, so the largest divisor tried is 10. It tests d = 2 through 10, which is 9 candidates. None divides 101, so 101 is prime.