Browse Source Download (without any required ccan dependencies)

Module:

eratosthenes

Summary:

Sieve of Eratosthenes

Author:

David Gibson <david@gibsond.dropbear.id.au>

Dependencies:

Description:

This code implements Eratosthenes' Sieve for efficiently finding small prime numbers (in this context anything less than several billion is "small").

Example:

#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;
}

License:

LGPL (v2.1 or any later version)