DragonFly On-Line Manual Pages
primegen(3) DragonFly Library Functions Manual primegen(3)
NAME
primegen - enumerate small primes
SYNTAX
#include <primegen.h>
void primegen_init(&pg);
uint64 primegen_next(&pg);
uint64 primegen_peek(&pg);
uint64 primegen_count(&pg,bound);
void primegen_skipto(&pg,bound);
struct primegen pg;
uint64 bound;
DESCRIPTION
The primegen library generates prime numbers in order: 2, 3, 5, 7, etc.
It can generate primes as large as 10^15.
The simplest way to use primegen is to call primegen_init and then call
primegen_next repeatedly. The first call to primegen_next will return
2; the next call will return 3; the next call will return 5; and so on.
At each moment the next few primes to be returned by primegen_next are
buffered inside pg. primegen_init initializes pg with the first few
primes, starting at 2. You must initialize pg with primegen_init
before using pg with any of the other primegen routines. You may call
primegen_init again to start over at 2.
primegen_next returns the next buffered prime and removes that prime
from pg. primegen_peek returns the next buffered prime but does not
change pg.
primegen_skipto discards all primes up to bound. It has the same
effect as
while (primegen_peek(&pg) < bound)
primegen_next(&pg);
primegen_count discards all primes up to bound and returns the number
of primes discarded.
You can set up several independent generators; all primegen data is
stored in pg. Beware, however, that each struct primegen uses quite a
bit of memory.
SEE ALSO
primes(1), primespeed(1)
primegen(3)