1.1.8 · D3Arithmetic & Number Systems

Worked examples — Prime numbers — Sieve of Eratosthenes, primality testing

2,651 words12 min readBack to topic

Before we start, one reminder of the notation we lean on, built from zero:


The scenario matrix

Every case this topic can throw at you falls into one of these cells. The worked examples below are tagged with the cell they cover, and together they hit all of them.

Cell Case class What makes it tricky Example
A Degenerate input: or Definition edge — "neither prime nor composite" Ex 1
B Smallest primes no divisor to test Ex 2
C Genuine prime, mid-size Must go all the way to Ex 3
D Sneaky composite , both odd "Feels prime", small-prime tests miss it Ex 4
E Perfect square The divisor sits exactly at Ex 5
F Even / trivially composite First test kills it Ex 6
G Full Sieve harvest up to Where to start/stop crossing Ex 7
H Real-world word problem Translate → primality Ex 8
I Exam twist (count divisors / limiting) Reason about structure, not brute force Ex 9

Ex 1 — Cell A: the degenerate inputs and


Ex 2 — Cell B: the smallest primes and


Ex 3 — Cell C: a genuine mid-size prime,


Ex 4 — Cell D: the sneaky composite

This is the trap the parent warned about, made concrete.

Figure — Prime numbers — Sieve of Eratosthenes, primality testing

The figure lays the candidate divisors on a number line. The dashed plum line marks . The teal dot at is the smaller factor and the orange dot at is the larger factor — notice sits just left of the plum line and sits right of it. The arrow shows exactly where a rushed "gut" test stops (after ), one prime short of the divisor that would have unmasked . Keep that picture in mind as we walk the steps.


Ex 5 — Cell E: a perfect square,


Ex 6 — Cell F: trivially composite,


Ex 7 — Cell G: full Sieve harvest up to

The Sieve isn't a per-number test — it paints out whole arithmetic ladders at once. Watch each colour.

Figure — Prime numbers — Sieve of Eratosthenes, primality testing

Ex 8 — Cell H: real-world word problem


Ex 9 — Cell I: exam twist (count divisors without brute force)


Recall

Recall Which cell did each example patch?
  • and ::: Cell A — fail the gate; neither prime nor composite.
  • ::: Cell B — empty test range, auto-prime.
  • ::: Cell C — real prime, test primes up to .
  • ::: Cell D — , the "don't stop early" trap.
  • ::: Cell E — , factor sits on ; use , not .
  • ::: Cell F — even, dies on first test.
  • Sieve to ::: Cell G — sieve with only; primes.
  • Gears ::: Cell H — LCM because .
  • ::: Cell I — divisor formula; so composite.

Connections