here's a simple C program i just wrote to find the nth prime number.
On my computer it's this fast:
This is my computer btw:
How fast is your computer?
Code:
#include <stdio.h> #include <stdlib.h> #include <time.h> long *primes; int numfound; int isprime(long totest) { int i=0; for(i=0; primes[i]*primes[i]<=totest; i++) { if(totest%primes[i]==0) return 0; if(i>=numfound) return 1; } return 1; } int main(int argc, char** argv) { int tofind; long totest; time_t start, end; if(argc!=2) exit(EXIT_FAILURE); tofind=atoi(argv[1]); numfound=1; primes=malloc(sizeof(long)); primes[0]=2; totest=2; start=clock(); while(numfound<tofind) { totest++; if(isprime(totest)) { numfound++; primes=realloc(primes, numfound*sizeof(long)); primes[numfound-1]=totest; } } end=clock(); printf("prime number %d: %ld\n", numfound, totest); printf("total time: %.2f\n\n", (end-start)/(float)CLOCKS_PER_SEC); free(primes); return EXIT_SUCCESS; }
Code:
╭─[/data/software/c]─[howie@howie-desktop]─[0]─[111] ╰─[:)] % ./primefinder 1000000 prime number 1000000: 15485863 total time: 7.65
Code:
╭─[/data/software/c]─[howie@howie-desktop]─[0]─[112] ╰─[:)] % cat /proc/cpuinfo | grep -m1 "model name" model name : AMD Phenom(tm) II X4 965 Processor
Comment