Sub topic 2
What Makes a Number Prime
This chapter assumes you know how the modulo operator works (a % b gives the remainder when a is divided by b) and can read short Python functions that use loops and list comprehensions. The previous chapter used remainders to define divisibility. This chapter uses divisibility to sort the integers into primes, composites, and a few special cases.
The definition
An integer d divides n when n % d == 0. Every integer n > 1 has at least two positive divisors: 1 and n itself. These are called the trivial divisors.
- A prime is an integer greater than 1 whose only positive divisors are 1 and itself. It has exactly two positive divisors.
- A composite is an integer greater than 1 that has at least one divisor
dwith1 < d < n. Equivalently, it can be written asa * bwith bothaandbbetween 2 andn - 1. - 0 and 1 are neither prime nor composite. Negative integers are also left out of the classification. Primality is defined only for integers greater than 1.
The phrase "exactly two divisors" carries the whole definition. The number 1 has one positive divisor, so it fails. Every integer from 2 upward has either exactly two divisors or more than two, so each one is either prime or composite.
Small examples
| n | Positive divisors | Classification |
|---|---|---|
| 1 | 1 | neither |
| 2 | 1, 2 | prime |
| 3 | 1, 3 | prime |
| 4 | 1, 2, 4 | composite |
| 5 | 1, 5 | prime |
| 6 | 1, 2, 3, 6 | composite |
| 9 | 1, 3, 9 | composite |
| 11 | 1, 11 | prime |
| 12 | 1, 2, 3, 4, 6, 12 | composite |
Two facts follow from the table:
- 2 is the only even prime. Every even number greater than 2 has 2 as a nontrivial divisor.
- Odd does not mean prime. The number 9 is odd and equals
3 * 3. The number 15 equals3 * 5.
The definition as code
The most literal translation of the definition counts every divisor from 1 to n. It is slow, but it makes a good reference implementation for testing faster versions later.
def divisors(n):
return [d for d in range(1, n + 1) if n % d == 0]
def classify(n):
if n < 2:
return "neither"
return "prime" if len(divisors(n)) == 2 else "composite"
print([(n, classify(n)) for n in range(10)])
# [(0, 'neither'), (1, 'neither'), (2, 'prime'), (3, 'prime'), (4, 'composite'),
# (5, 'prime'), (6, 'composite'), (7, 'prime'), (8, 'composite'), (9, 'composite')]This version does n modulo operations for every number it checks. Later chapters cut that to about the square root of n for one number, and then handle whole ranges at once with a sieve.
Common errors
Other mistakes come up often:
- Using the wrong divisor range. If a loop runs
range(1, n), then 1 always dividesnand every number looks composite. If it runsrange(2, n + 1), thenndivides itself and the same thing happens. When you look for nontrivial divisors, the range must exclude both 1 andn. - Assuming odd means prime. Code that checks only
n % 2misses 9, 15, 21, 25, and every other odd composite. - Forgetting that 2 is prime. Shortcuts that reject every even number also reject 2.
Why 1 is excluded
Excluding 1 is a deliberate choice. The next chapter shows that every integer greater than 1 factors into primes in exactly one way. If 1 counted as prime, then 6 = 2 * 3 = 1 * 2 * 3 = 1 * 1 * 2 * 3 would have infinitely many factorizations, and the uniqueness statement would fail.
Exercise
Classify 0, 1, 2, 21, 29, and 49 as prime, composite, or neither, and give a nontrivial divisor for each composite.
Show answer
0 and 1 are neither. 2 is prime. 21 is composite (3 divides it, 21 = 3 7). 29 is prime. 49 is composite (7 divides it, 49 = 7 7).
Exercise
A student writes def is_prime(n): return all(n % d != 0 for d in range(2, n)). Which inputs give the wrong answer, and how do you fix it?
Show answer
For n = 0 and n = 1, range(2, n) is empty, so all() returns True and both are wrongly reported as prime. Negative inputs fail the same way. Add if n < 2: return False before the check. For n >= 2 the function is correct, though slow.