User Tools

Site Tools


algorithms_primes

This is an old revision of the document!


Algorithms: Prime Numbers

Prime Sieves

Prime sieves are common devices for generating prime numbers in a given range. These lists can be used for quick verification of relatively small prime numbers (typically ranging up to 106 or greater), in particular when many such verifications may be necessary.

Etymology: A sieve1) is a physical metaphor (i.e., a mesh bowl in a kitchen) for an algorithmic technique used in number theory to “sift out” a class of numbers in a given set or range.

Sieve of Eratosthenes

sieve.py
def sieve(n):
    primes = [True] * (n+1)
    primes[0] = primes[1] = False
    for i in range(2, int(sqrt(n))+1):
        for j in range(i**2, n+1, i):
            primes[j] = False
    return [i for i in range(len(primes)) if primes[i]]
 
primes = sieve(1000000)
sieve.cpp
#include <bitset>
#include <cmath>
#include <iostream>
#include <vector>
 
using namespace std;
 
const int MAX_PRIMES=10000000;
 
vector<int>sieve(int n) {
  bitset<MAX_PRIMES+1> nums;
  nums.set();
  nums[0] = nums[1] = false;
  for(int i=2; i<=sqrt(n)+1; i++)
    for(int j=i*i; j<n+2; j+=i)	
      nums[j] = false;
 
  vector<int> primes;
  for(int i=0; i<nums.size(); i++)
    if(nums[i] == true)	
      primes.push_back(i);
 
  return primes;
}  
 
int main() {
  vector<int> primes = sieve(MAX_PRIMES);
 
  return 0;
}

Design Principles

Operation Sieve of Eratosthenes is quite simple. The sieve starts by assuming all numbers in a given range (2..n) are prime (True values in our list). Then for each number, i, from (2..sqrt(n)) we eliminate any multiples of i by setting them to False.2) After completion, the True values indicate indices that are prime (the list of which is generated by the final list comprehension in the return statement).

Analysis

The Sieve of Eratosthenes is not the fastest prime sieve available, but it is quick to code, easy to understand and modify, and sufficiently fast for some practical applications.

Time Complexity: O((n log n)(log log n))3)
Space Complexity: O(n)

Benchmarks

This implementation (Python3) should work reliably for primes up to 106 in competition settings.

Source

Implementations are from the pseudocode provided in the Wikipedia article on the Sieve of Eratosthenes4).

1)
Pronounced like “give” not “sleeve”.
2)
We start the inner loop at i2 to avoid many repeated assignments to the same array locations.
4)
Retrieved 08/08/2018
algorithms_primes.1533764253.txt.gz · Last modified: 2018/08/08 16:37 by jguerin