Sub topic 3
Primes, composites and coprime numbers
This chapter assumes you can do integer division with remainder and know what a factor and a multiple are (from the chapter on divisibility basics). It also assumes you are comfortable with exponent notation. Primes are the atoms that every later tool in this tutorial is built from: prime factorization, factor counting, HCF and LCM.
Prime and composite numbers
A prime is an integer greater than 1 whose only positive factors are 1 and itself. A composite is an integer greater than 1 with at least one other factor. The number 1 is neither, because it has only one factor. This is a deliberate convention: it keeps prime factorization unique, since otherwise 6 = 2 × 3 = 1 × 2 × 3 = 1 × 1 × 2 × 3 and so on.
The facts below come up repeatedly in CAT questions.
- 2 is the only even prime. Every other even number has 2 as a factor besides 1 and itself.
- 3, 5, 7 and 11 are the primes you meet most often. Learn the full list below 100 by heart.
- Every composite number is a product of primes (the next chapter develops this).
- There are infinitely many primes, so no "largest prime" question can have a fixed answer.
The 25 primes below 100:
| Range | Primes |
|---|---|
| 1 to 20 | 2, 3, 5, 7, 11, 13, 17, 19 |
| 21 to 50 | 23, 29, 31, 37, 41, 43, 47 |
| 51 to 100 | 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 |
Testing whether a number is prime
You never need to test every smaller number. If n is composite, it can be written as n = a × b with 1 < a ≤ b. Then a × a ≤ a × b = n, so a ≤ √n. Therefore a composite number always has a prime factor no larger than its square root, and you only need to test the primes up to √n.
Worked example: is 391 prime?
- √391 is a little under 20 (since 20² = 400), so test the primes 2, 3, 5, 7, 11, 13, 17, 19.
- 391 is odd, its digit sum 3 + 9 + 1 = 13 is not a multiple of 3, and it does not end in 0 or 5.
- 391 ÷ 7 leaves remainder 6 (7 × 55 = 385). 391 ÷ 11 leaves 6 (11 × 35 = 385). 391 ÷ 13 leaves 1 (13 × 30 = 390).
- 391 ÷ 17 = 23 exactly, so 391 = 17 × 23 and it is composite.
To find the bound quickly, use the nearest perfect square above n. For n up to 400 you test primes up to 19. For n up to 900 you test primes up to 29.
The same procedure as code:
from math import isqrt
def is_prime(n):
if n < 2:
return False
if n % 2 == 0:
return n == 2
for d in range(3, isqrt(n) + 1, 2):
if n % d == 0:
return False
return True
print([n for n in range(1, 31) if is_prime(n)])
print(is_prime(391), is_prime(1147), is_prime(1009))The first line prints [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]. The second prints False False True, because 391 = 17 × 23 and 1147 = 31 × 37.
The 6k ± 1 pattern and its limits
Every integer has the form 6k, 6k+1, 6k+2, 6k+3, 6k+4 or 6k+5. Numbers of the form 6k, 6k+2 and 6k+4 are even, and 6k+3 is a multiple of 3. So every prime greater than 3 has the form 6k − 1 or 6k + 1.
The converse is false. 25 = 6 × 4 + 1 and 35 = 6 × 6 − 1 are both composite. The pattern only narrows the candidates. It does not certify a prime.
CAT questions use this in the form "p is a prime greater than 3, so what is the remainder of p² when divided by 6 (or 12, or 24)?" Since p = 6k ± 1, p² = 36k² ± 12k + 1, which leaves remainder 1 when divided by 6 and by 12. Check with p = 5: 25 = 2 × 12 + 1.
Coprime numbers
Two integers are coprime (also called relatively prime) if their HCF is 1, meaning they share no prime factor. Coprimality is about the pair, not about either number alone.
- 8 and 15 are coprime, although neither is prime.
- Any two distinct primes are coprime.
- Any two consecutive integers n and n + 1 are coprime. Any common divisor of both would divide their difference, which is 1.
- A prime p and another number m are coprime unless p divides m.
- 1 is coprime to every integer.
For more than two numbers, there are two different conditions. A set is coprime (setwise) if the HCF of all of them is 1. A set is pairwise coprime if every pair is coprime. Pairwise coprime implies setwise coprime, but not the other way round.
from itertools import combinations
from functools import reduce
from math import gcd
def pairwise_coprime(nums):
return all(gcd(a, b) == 1 for a, b in combinations(nums, 2))
nums = [6, 10, 15]
print(reduce(gcd, nums), pairwise_coprime(nums)) # 1 False
print(pairwise_coprime([8, 9, 25])) # TrueThe numbers 6, 10 and 15 have overall HCF 1, but gcd(6, 10) = 2, gcd(6, 15) = 3 and gcd(10, 15) = 5.
Why coprimality matters for divisibility
Coprime numbers let you split a divisibility test into smaller tests. If a and b are coprime and both divide N, then their product ab divides N. This is why you can test divisibility by 12 by checking 3 and 4, and by 6 by checking 2 and 3.
A second, related fact is useful in proofs and in "find the unknown" questions. If a divides bc and a is coprime to b, then a divides c. For example, if 7 divides 12m, then since 7 is coprime to 12, 7 divides m.
Common traps
| Trap | Correct view |
|---|---|
| Treating 1 as prime | 1 is neither prime nor composite |
| Treating 2 as non-prime because it is even | 2 is the only even prime |
| Assuming coprime means prime | 9 and 16 are coprime and both composite |
| Assuming every number of the form 6k ± 1 is prime | 25 and 35 are counterexamples |
| Testing divisors all the way to n | Stop at √n, and only use primes |
| Confusing setwise and pairwise coprime | 6, 10, 15 have HCF 1 but are not pairwise coprime |
| Checking divisibility by 12 via 2 and 6 | 2 and 6 share the factor 2, so use 3 and 4 |
A short sieve for lists of primes
When a question needs all primes up to some limit, the sieve of Eratosthenes is faster than testing each number. Start with all numbers from 2 up. Take the smallest unmarked number, which is prime, and cross out its multiples starting at its square. Repeat until the next prime exceeds the square root of the limit.
def primes_up_to(limit):
is_p = [True] * (limit + 1)
is_p[0:2] = [False, False]
p = 2
while p * p <= limit:
if is_p[p]:
for m in range(p * p, limit + 1, p):
is_p[m] = False
p += 1
return [n for n, ok in enumerate(is_p) if ok]
ps = primes_up_to(100)
print(len(ps), ps[-1]) # 25 97Practice
Exercise
Is 1147 prime? State which primes you need to test and give the factorization if it is composite.
Show answer
√1147 is a little under 34 (34² = 1156), so test the primes up to 31. The primes 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29 do not divide 1147, but 31 does: 31 × 37 = 1147. So 1147 is composite.
Exercise
Find all primes p such that p, p + 2 and p + 4 are all prime.
Show answer
Among any three numbers p, p + 2, p + 4, one is a multiple of 3, because they leave three different remainders on division by 3 unless p is already a multiple of 3. That multiple of 3 must equal 3 to be prime. This gives the single triple 3, 5, 7. For p > 3, one of the three is a multiple of 3 larger than 3, hence composite.
Exercise
A prime p is greater than 3. What remainder does p² leave when divided by 24?
Show answer
p = 6k ± 1 is odd and not a multiple of 3. Then p² − 1 = (p − 1)(p + 1) is a product of two consecutive even numbers, so it is divisible by 8, and one of p − 1, p, p + 1 is a multiple of 3, which must be p − 1 or p + 1. So 24 divides p² − 1, and the remainder is 1. Check: 5² = 25 = 24 + 1.
Exercise
How many integers from 1 to 30 are coprime to 30?
Show answer
30 = 2 × 3 × 5, so a number is coprime to 30 exactly when it is divisible by none of 2, 3 and 5. They are 1, 7, 11, 13, 17, 19, 23 and 29, so the answer is 8.