Browse Source Download (without any required ccan dependencies)
eratosthenes
Sieve of Eratosthenes
David Gibson <david@gibsond.dropbear.id.au>
This code implements Eratosthenes' Sieve for efficiently finding small prime numbers (in this context anything less than several billion is "small").
#include <ccan/eratosthenes/eratosthenes.h>
int main(int argc, char *argv[])
{
struct eratosthenes s;
unsigned long p;
eratosthenes_init(&s);
eratosthenes_sieve(&s, atol(argv[1]));
while ((p = eratosthenes_nextprime(&s, p)) != 0) {
printf("%ld\n", p);
}
return 0;
}
LGPL (v2.1 or any later version)