Luke a Pro

Luke Sun

Developer & Marketer

šŸ‡ŗšŸ‡¦

Primes & Sieves

| , 4 minutes reading.

Primes & Sieves: The Atoms of Logic

The Story: The Greek Filter

In 200 BC, Eratosthenes of Cyrene, the head of the Great Library of Alexandria, was fascinated by prime numbers—numbers that have no friends (no divisors other than 1 and themselves).

He realized that searching for primes was like searching for gold in a river. You don’t pick up every rock to see if it’s gold. You build a Sieve.

Imagine a grid of numbers from 2 to 100.

  1. Start at 2. It’s a prime. Now, reach out and ā€œcross outā€ every multiple of 2 (4, 6, 8…). They are definitely NOT primes.
  2. Move to 3. It’s a prime. Cross out every multiple of 3 (6, 9, 12…).
  3. Skip 4 (it’s already crossed out).
  4. Move to 5…

By the time you finish, only the pure ā€œGoldā€ remains. This is the Sieve of Eratosthenes. It remains one of the most efficient ways to find all small primes.

Why do we need it?

  • Cryptography: RSA encryption is based on the difficulty of factoring the product of two massive primes. Finding these primes is the first step in creating a secure internet.
  • Hashing: Hash table sizes are often prime numbers because they minimize ā€œCollisionsā€ in the modulo operation.
  • Game Mechanics: Using primes for spawn rates or periodic events to prevent them from synchronizing (overlapping) too often.

How the Algorithm ā€œThinksā€

The Sieve is an Elimination Engine.

  1. Assumed Prime: We start by assuming every number is a prime (a boolean array of True).
  2. The Strike-Through: Starting from 2, if a number is still True, we iterate through its multiples and set them to False.
  3. Optimization (The Square Rule): You don’t need to start crossing out from 2imesP2 imes P. You can start from PimesPP imes P, because everything smaller has already been handled by smaller primes.
  4. The Boundary: To find all primes up to NN, you only need to process numbers up to N\sqrt{N}.

Engineering Context: The Large Prime Problem

The Sieve is great for finding all primes up to 10 million. But what if you need a single 2048-bit prime for a TLS certificate?

  • The Sieve would require more memory than exists in the universe.
  • Instead, engineers use Probabilistic Primality Tests (like Miller-Rabin). These tests don’t prove a number is prime with 100% certainty, but they can prove it with 99.9999999% certainty in milliseconds. In engineering, ā€œGood enoughā€ is the standard for speed.

Implementation (Python)

def sieve_of_eratosthenes(n):
    # 1. Create a sieve and assume everything is prime
    is_prime = [True] * (n + 1)
    is_prime[0] = is_prime[1] = False # 0 and 1 are not primes
    
    # 2. Iterate up to sqrt(n)
    p = 2
    while p * p <= n:
        if is_prime[p]:
            # 3. Cross out all multiples of p starting from p*p
            for i in range(p * p, n + 1, p):
                is_prime[i] = False
        p += 1
    
    # 4. Collect the survivors
    return [i for i, prime in enumerate(is_prime) if prime]

# Example
print(f"Primes up to 50: {sieve_of_eratosthenes(50)}")

Summary

Primes teach us that complexity is built from simplicity. By filtering out the noise of composites, we arrive at the fundamental building blocks of math. It reminds us that in security and engineering, the strongest foundations are often the most elementary ones.