Prime Stream is designed to generate 64 bit prime numbers as quickly as possible to help factor large numbers. I originally wrote this code to help me solve Project Euler problem #3. One thing to note about it is that when it is initialized, it generates another thread to generate the prime numbers and store in the buffer, so this code will take advantage of multicore systems.
Code:
primestream.c (version 0.1)
Usage:
#include <stdio.h>
#include "primestream.c"
main() {
primestream *foo = ps_init();
while(1) {
printf("%llu\n", ps_getprime(foo)); // %llu is unsigned long long, uint64_t
}
}
Extensions:
There still needs to be a way to stop the extra thread and free all the resources if the prime generator is no longer needed.
The sieve used is still not optimal, as it iterates along the list of already known primes, it should give up after it has reached the square root of the number being tested.
The use of a linked list to store the known prime numbers may not be optimal, but is good enough for me.
NOTE: Patches are perfectly welcome! KennethFinnegan2007 at gmail dot com
Sources:
Project Euler
Wikipedia
Comments (0)
You don't have permission to comment on this page.