#include "shared.h" int ptest (unsigned long num, char * filename) { unsigned long primes[num]; // array holding primes unsigned long pfound, cnum, count; unsigned long p = num; FILE *fp; /* set up search */ pfound = 1; // found primes cnum = 3; // current number primes[0] = 2; // give the first prime fp = fopen(filename, "w"); fprintf(fp, "%d\n", 2); // print the first prime while (pfound != p) { /* check % != 0 for each found prime */ count = 0; for (int i = 0; i < pfound; i++) { if ( cnum % primes[i] == 0) { // speed increase -- HOMEWORK 6 break; } else count++; } /* if % !=0 for all found primes, add cnum to list */ if (count == pfound) { primes[pfound] = cnum; fprintf(fp, "%d\n", cnum); pfound++; } cnum = cnum + 2; // don't check evens } fclose(fp); printf("last prime: %d\n", primes[p-1]); // print last prime return 0; }