#include #include #include "math.h" /* By Steven Miller, original version '95-'96, modified */ /* version '01. The goal of this program is to read in a */ /* list of primes generated by the program genprime.c and */ /* saved in a file named primedat.fil. */ void main() { long numprimesless, length, j; long primes[50002]; long sqrtpr[50002]; /* see if you can have larger sizes */ /* these are all the variables we need for reading. */ /* genprime.c calculates all the primes up to 50002 */ /* and saves them in primedat.fil. we of course don't */ /* need primes to be of size 50002 -- we expect it will */ /* be of size 50002/log50002 = approx = 5000. The true */ /* answer is about 5134. So, really, if we can work wi' */ /* arrays this size, we can calculate and store primes up */ /* to 10^6. */ FILE *fpr; fpr = fopen("primedat.fil", "r"); /* opens for reading */ /* the structure of the file primedat.fil is as follows: the first */ /* entry is the number of primes <= 50002 -- we read that into */ /* numprimesless. the next set of numbers are the primes (in order) */ /* starting wi' 2 and ending wi' the numprimesless prime. we read */ /* these primes into primes[]. then the next stuff is for sqrts, to */ /* optimize searching. */ fscanf(fpr, "%ld ", &numprimesless); for (j = 1; j <= numprimesless; ++j) { fscanf(fpr, "%ld ", &primes[j]); } fscanf(fpr, "%ld ", &length); for (j = 1; j <= length; ++j) { fscanf(fpr, "%ld ", &sqrtpr[j]); } fclose(fpr); /* this part of the program is for testing / debugging */ printf("The number of primes is %ld\n", numprimesless); for (j = 1; j <= numprimesless; j++) printf("prime[%ld] = %ld\n", j, primes[j]); /* this will print all the primes we've saved. the */ /* number of primes is numprimesless. so, for example, */ /* we use genprime.c to calculate all the primes up to */ /* 50002; there are numprimeless such primes, which is */ /* 5134 or something like that. we then read them in to */ /* the array primes[], and print */ }