Why it matters

Linear sieve is elegant O(n) alternative. Understanding shapes optimization.

Advertisement

The architecture

Iterate i from 2 to n.

If not marked: prime.

Mark i * prime for primes <= smallest prime factor of i.

Linear sieve structureIterate i2 to nMark i * primeonly up to SPF(i)Each compositemarked onceAlso gives multiplicative function values (Euler phi, Mobius) alongside
Linear sieve.
Advertisement

How it works end to end

Linear time.

Yields smallest prime factor.

Multiplicative function computation.