/* Calculates number of primes between 1 and 20 million. * CS 226 Lab 6 */ #include #include #include #include #include //returns whether num is prime bool isPrime(long num) { int limit = (int) sqrt(num); for(long i=2; i<=limit; i++) { if(num % i == 0) return false; } return true; } // start should be odd. void count_primes (long *pCount, long start, long finish) { while(start < finish) { if(isPrime(start)) { ++*pCount; } start += 2; } } int main () { long pCount = 1; //(starting with 2) count_primes (&pCount, 3, 20000000); printf ("%ld primes found", pCount); }