/* File: primes1.c Single thread version to see how slow compile with: g++ primes1.c -o primes1 to get timing, run with: time ./primes1 */ #include #include // deliberately slow algorithm for checking primality // int isPrime (unsigned int n) { unsigned int m = sqrt(n) ; for (unsigned int k=2 ; k <= m ; k++) { if (n % k == 0) return 0 ; } return 1 ; } int main() { int numPrimes=0 ; const int n=10000000 ; // 10 million for(int i=2 ; i < n ; i++) { if ( isPrime(i) ) numPrimes++ ; } printf("Number of primes less than %d is %d\n", n, numPrimes) ; return 0 ; }