1.1.8Arithmetic & Number Systems

Prime numbers — Sieve of Eratosthenes, primality testing

1,732 words8 min readdifficulty · medium4 backlinks

1. What is a prime? (WHAT)


2. Trial-division primality test — derive it from scratch (HOW + WHY)

Goal: decide if nn is prime.

Naive: test every dd from 22 to n1n-1. If none divides nn, it's prime. Correct but slow.

The key optimisation — why n\sqrt{n}?

Suppose nn is composite, so n=a×bn = a \times b with 1<ab<n1 < a \le b < n.

So the test becomes:

check d=2,3,4,,n\text{check } d = 2, 3, 4, \dots, \lfloor\sqrt{n}\rfloor

Cost drops from O(n)O(n) to O(n)O(\sqrt n).


3. Sieve of Eratosthenes — build it from first principles

Idea: Instead of testing numbers one-by-one, cross out all multiples of each prime. What remains is prime.

Figure — Prime numbers — Sieve of Eratosthenes, primality testing

4. Common mistakes (Steel-man → Fix)


5. Active recall

Recall Try before revealing
  • Why is the trial-division bound n\sqrt n?
  • Why does the sieve start crossing at p2p^2?
  • Give a composite that fools "check only 2,3,5".

Answers: smaller factor of a pair n\le\sqrt n; multiples <p2<p^2 already crossed by smaller primes; 49=7249=7^2 or 77=71177=7\cdot11.

Recall Feynman: explain to a 12-year-old

Imagine numbers standing in a line. A prime is a kid who can only make a straight single line of blocks — never a neat rectangle. The Sieve is a game: 2 tells all its "friends" (4, 6, 8…) to sit down. Then 3 does the same, then 5. Whoever is still standing at the end could never sit — they're the primes! And you only need the first few callers, because tall rectangles were already knocked down by short ones.


6. The 80/20 core

  • Prime = exactly two divisors. 1 is neither.
  • Primality: check divisors 2n2\to\lfloor\sqrt n\rfloor.
  • Sieve: cross multiples of each pp from p2p^2; stop at pNp\le\sqrt N; survivors are prime.

Define a prime number
A natural number p>1p>1 whose only positive divisors are 1 and pp (exactly two distinct divisors).
Is 1 prime, composite, or neither?
Neither — it has only one divisor, and allowing it would break unique factorisation.
Why need we only trial-divide up to n\sqrt n?
If n=abn=ab with aba\le b, then ana\le\sqrt n; a divisor above n\sqrt n forces a partner below it.
In the Sieve, from which multiple do we start crossing out pp?
From p2p^2 — smaller multiples are already crossed by smaller primes.
In the Sieve up to NN, when do we stop sieving?
When p2>Np^2>N (i.e. p>Np>\sqrt N); remaining numbers are automatically prime.
Give a composite that survives "test only 2,3,5".
49=7249=7^2 (or 77=7×1177=7\times11, 91=7×1391=7\times13).
List all primes below 30.
2,3,5,7,11,13,17,19,23,29.
Time complexity of trial division per number?
O(n)O(\sqrt n).
State the Fundamental Theorem of Arithmetic.
Every integer >1>1 factors into primes uniquely (up to order).
Is 91 prime?
No, 91=7×1391=7\times13.

Connections

Concept Map

only divisors

has divisor d

excluded to keep unique

building blocks of

factors as

smaller factor bound

justifies

cost

test one number

cross out multiples of

start crossing at

smaller multiples already gone

survivors are

Prime p greater than 1

1 and p itself

Composite n

d with 1 less than d less than n

Number 1

Fundamental Theorem of Arithmetic

n = a times b

a less than or equal sqrt n

Trial-division test

O of sqrt n

Sieve of Eratosthenes

p squared

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, prime number woh hota hai jiske sirf do divisors hote hain — 1 aur khud woh number. Jaise 7 ko sirf 1 aur 7 hi divide karte hain, isliye 7 prime hai. Agar kisi number ke beech mein koi aur factor mil jaaye (jaise 91 = 7×13), toh woh composite hai. Aur haan, 1 na prime hai na composite — kyunki uske sirf ek hi divisor hai, aur agar 1 ko prime maan lein toh factorisation unique nahi rehti.

Primality test ka smart trick: kisi number nn ko prime check karne ke liye 2 se lekar n1n-1 tak sab check karne ki zaroorat nahi. Sirf n\sqrt n tak check karo. Kyun? Kyunki agar n=a×bn = a\times b hai aur aba \le b, toh chhota factor aa kabhi n\sqrt n se bada nahi ho sakta. Toh n\sqrt n tak koi divisor na mile, matlab number prime hai. 97 ke liye sirf 2,3,5,7 check karo — done!

Sieve of Eratosthenes ek super-fast tareeka hai saare primes ek saath nikalne ka. 2 se 30 tak list banao. Pehle 2 ke multiples kaato, phir 3 ke, phir 5 ke — bas 30\sqrt{30} tak. Har prime pp ke multiples ko p2p^2 se kaatna shuru karo (kyunki usse chhote multiples pehle hi kat chuke hain). Jo bache reh jaayein — wahi primes hain! Yeh trick number theory, cryptography, aur coding problems mein bahut kaam aati hai, isliye deeply samajhna zaroori hai.

Go deeper — visual, from zero

Test yourself — Arithmetic & Number Systems

Connections