Sub topic 2
What Makes a Number Prime
This chapter turns the word "prime" into a precise definition you can check in code. It assumes you know integer division and the modulo operator %, and that you can read short Python functions with loops and list comprehensions. The previous chapter covered divisibility. Here, "d divides n" means n % d == 0.
The Definition
A prime number is an integer greater than 1 whose only positive divisors are 1 and itself. A composite number is an integer greater than 1 that has at least one other positive divisor.
Both definitions start with "greater than 1." That condition is part of the definition, and it puts 0 and 1 in neither category. Every integer n ≥ 2 falls into exactly one of the two classes:
- Prime: the positive divisors of n are exactly {1, n}.
- Composite: there is some d with 1 < d < n and d divides n.
A shorter form of the definition is often easier to work with: a prime is a positive integer with exactly two positive divisors. This version excludes 1 automatically, because 1 has only one positive divisor.
Equivalent ways to say "composite"
The following statements say the same thing for an integer n ≥ 2:
- n has a divisor d with 1 < d < n.
- n = a × b for some integers a and b with 1 < a, b < n.
- n has more than two positive divisors.
Moving from (1) to (2) works because if d divides n, then n = d × (n / d). Since 1 < d < n, the cofactor n / d also lies strictly between 1 and n. Such a pair (a, b) is called a nontrivial factorization, and it is a witness that n is composite. To prove a number composite, you only need to show one witness. To prove a number prime, you have to rule out every possible witness. That asymmetry shows up in every primality algorithm in this tutorial.
The First Few Integers, Classified
| n | Positive divisors | Count | Class |
|---|---|---|---|
| 1 | 1 | 1 | neither |
| 2 | 1, 2 | 2 | prime |
| 3 | 1, 3 | 2 | prime |
| 4 | 1, 2, 4 | 3 | composite |
| 5 | 1, 5 | 2 | prime |
| 6 | 1, 2, 3, 6 | 4 | composite |
| 7 | 1, 7 | 2 | prime |
| 8 | 1, 2, 4, 8 | 4 | composite |
| 9 | 1, 3, 9 | 3 | composite |
| 10 | 1, 2, 5, 10 | 4 | composite |
| 11 | 1, 11 | 2 | prime |
| 12 | 1, 2, 3, 4, 6, 12 | 6 | composite |
A few patterns stand out in the table:
- 2 is the only even prime. Every larger even number has 2 as a divisor strictly between 1 and itself.
- 9 is odd but composite. Being odd is necessary for a prime above 2, but it is not enough.
- Squares of primes such as 4 and 9 have exactly three divisors: 1, p, and p². Squares come up again when the sieve starts each pass at p squared.
The Definition as Code
The most direct implementation counts divisors. It is slow, since it checks every candidate from 1 to n, but each line matches a phrase in the definition. That makes it a useful reference to test faster versions against.
def divisors(n):
"""Return the positive divisors of a positive integer n, in increasing order."""
if n < 1:
raise ValueError("divisors() expects a positive integer")
return [d for d in range(1, n + 1) if n % d == 0]
def classify(n):
"""Return 'prime', 'composite', or 'neither' for an integer n."""
if n < 2:
return "neither"
return "prime" if len(divisors(n)) == 2 else "composite"
for n in range(1, 13):
print(n, divisors(n), classify(n))
print(classify(0), classify(-7)) # neither neitherThe output reproduces the table above. classify checks n < 2 before it calls divisors. That guard is the "greater than 1" clause of the definition written as code.
Finding a witness
A second version searches for a nontrivial factorization and returns it. A witness is more useful than a plain yes or no, because it tells you why a number is composite.
def nontrivial_factorization(n):
"""Return (a, b) with a * b == n and 1 < a <= b < n, or None if none exists."""
for a in range(2, n):
if n % a == 0:
return a, n // a
return None
def is_prime_by_witness(n):
return n >= 2 and nontrivial_factorization(n) is None
print(nontrivial_factorization(91)) # (7, 13)
print(nontrivial_factorization(97)) # None
print(is_prime_by_witness(1)) # False
print(is_prime_by_witness(97)) # TrueThe loop goes up in order, so the first divisor it finds is the smallest divisor greater than 1. Call it a. Then a ≤ n / a, which is why the docstring can promise a <= b. This smallest divisor is always prime. If it were composite, it would have a smaller divisor greater than 1, and that divisor would also divide n, which contradicts a being the smallest. The fact that the smallest divisor greater than 1 of any n ≥ 2 is prime is the basis of the smallest-prime-factor table in the last chapter.
Classifying an Integer
Why 1 Is Not Prime
The definition is a choice, and older texts sometimes listed 1 as prime. Modern mathematics excludes it because including it would break the most useful fact about primes: every integer greater than 1 factors into primes in exactly one way, apart from the order of the factors. If 1 were prime, then 6 = 2 × 3 = 1 × 2 × 3 = 1 × 1 × 2 × 3, and so on, and "exactly one way" would fail. The next chapter states and uses that theorem. The point to take from here is that the "greater than 1" clause is what keeps prime factorization unique.
There is a second reason. A prime should be a number that cannot be split into smaller pieces under multiplication. The number 1 is the unit, the identity for multiplication. Multiplying by 1 changes nothing, so it adds no information to a factorization.
The Property That Makes Primes Special
The definition describes primes by what divides them. An equivalent and more powerful description looks at what primes divide:
Composite numbers fail this test. For example, 6 divides 4 × 9 = 36, but 6 divides neither 4 nor 9. Euclid's lemma is the step that turns "every number has some prime factorization" into "every number has exactly one." Its proof uses the greatest common divisor, so this tutorial treats it as a known fact.
Edge Cases and Common Errors
| Mistake | What goes wrong | Fix |
|---|---|---|
| Treating 1 as prime | Breaks unique factorization and gives off-by-one counts of primes | Require n ≥ 2 |
| Forgetting 2 | Code that skips all even numbers also skips 2 | Handle 2 before the odd-only loop |
| Checking `n % d` for d = n or d = 1 | Every n "has a divisor" and is reported composite | Search only 1 < d < n |
| Negative inputs | In Python, `-7 % 2 == 1`, so loops can quietly do the wrong thing | Define primality on positive integers and return False below 2 |
| Passing a float such as `7.0` | `range(2, 7.0)` raises `TypeError`; `7.5 % 2` is meaningful but irrelevant | Validate that the input is an `int` at the boundary |
| Using `bool` values | `True` is the int 1 in Python, so `classify(True)` returns `"neither"` | Usually harmless, but be aware of it if inputs come from comparisons |
What about negative numbers and 0?
In more abstract algebra, −7 counts as a prime "associate" of 7, because the two differ only by the unit −1. This tutorial follows the convention used in programming and elementary number theory: primality applies only to positive integers, and the primes are 2, 3, 5, 7, 11, and so on. Zero is divisible by every integer, so it is not prime, and it is not composite in the usual sense either. It sits outside the classification along with 1.
Cost of the Definition-Based Check
The divisors function performs n modulo operations, and nontrivial_factorization performs up to n − 2 of them for a prime input. For a 12-digit number that is about 10¹² operations, which takes hours in Python. Two later chapters fix this from different directions:
- Testing one number: you only need to search for witnesses up to √n, because in any factorization a × b = n, the smaller factor is at most √n.
- Testing every number up to a limit: the sieve of Eratosthenes skips per-number testing and crosses out multiples instead.
Keep classify around anyway. A slow but obviously correct version is the best way to test faster ones:
def reference_primes(limit):
return [n for n in range(limit) if classify(n) == "prime"]
print(reference_primes(30)) # [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]Exercise
How many positive divisors does p^k have when p is prime and k ≥ 1? Use your answer to explain why 4, 9, 25, and 49 each have exactly three divisors.
Show answer
The divisors of p^k are 1, p, p², …, p^k, so there are k + 1 of them. Each of 4 = 2², 9 = 3², 25 = 5², and 49 = 7² has k = 2, which gives 3 divisors. In fact, a number has exactly three divisors only when it is the square of a prime.
Exercise
A teammate writes def is_prime(n): return all(n % d != 0 for d in range(2, n)). Which inputs give the wrong answer, and what is the smallest fix?
Show answer
For n = 0 and n = 1 (and any negative n), range(2, n) is empty, so all(...) returns True and the function reports them as prime. The fix is return n >= 2 and all(n % d != 0 for d in range(2, n)).
Exercise
Show that 6 does not satisfy Euclid's lemma by finding a and b where 6 divides a × b but divides neither a nor b. Then explain in one sentence why a prime cannot fail the same way.
Show answer
Take a = 2 and b = 3 (or a = 4 and b = 9). 6 divides 6, but 6 divides neither 2 nor 3. A prime p has no factorization into smaller pieces that could be shared out between a and b, so if p divides a × b it must divide a or b, as Euclid's lemma states.
Exercise
Using nontrivial_factorization, explain why the first value a it returns for any composite n is always prime. Check this on n = 91 and n = 221.
Show answer
The loop returns the smallest divisor of n greater than 1. If that divisor were composite, it would have a smaller divisor greater than 1, which would also divide n and would have been found first. For 91 the function returns (7, 13), and 7 is prime. For 221 it returns (13, 17), and 13 is prime.