Efficient Prime Number Algorithms Guide | Generated by AI

Home 2026.02

Great to hear you’re thinking about efficient prime number algorithms—this is a classic and essential topic in competitive programming (CP)! Here’s a breakdown of the most common tasks and efficient methods, with Python examples.


1. Checking if a Number is Prime

Fast Method: Trial Division up to √n

def is_prime(n: int) -> bool:
    if n <= 1:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False
    for i in range(3, int(n**0.5) + 1, 2):
        if n % i == 0:
            return False
    return True

2. Generating All Primes up to s (Sieve of Eratosthenes)

Fast Method: Sieve of Eratosthenes

def sieve(s: int) -> list[int]:
    if s < 2:
        return []
    sieve = [True] * (s + 1)
    sieve[0] = sieve[1] = False
    for i in range(2, int(s**0.5) + 1):
        if sieve[i]:
            sieve[i*i : s+1 : i] = [False] * len(sieve[i*i : s+1 : i])
    return [i for i, is_prime in enumerate(sieve) if is_prime]

3. Typical CP Tasks Involving Primes

Common Problems:


4. Further Optimizations


Example: Prime Factorization

def prime_factors(n: int) -> list[int]:
    factors = []
    while n % 2 == 0:
        factors.append(2)
        n //= 2
    for i in range(3, int(n**0.5) + 1, 2):
        while n % i == 0:
            factors.append(i)
            n //= i
    if n > 2:
        factors.append(n)
    return factors

Summary Table

Task Method Time Complexity
Check if n is prime Trial division (√n) O(√n)
Generate primes up to s Sieve of Eratosthenes O(n log log n)
Prime factorization Trial division O(√n)

Your Thoughts?

You mentioned using multiples of existing primes to exclude non-primes—this is exactly what the Sieve of Eratosthenes does! For even faster results in CP, precompute primes and reuse them.

Would you like a deeper dive into any of these methods or a specific CP problem involving primes?


Back Donate