/* File: twinprimes1.c Slow single-threaded version to find a twin prime between begin and end. compile with: g++ twinprimes1.c -o twinprimes1 to get timing, run wiht: time ./twinprimes1 */ #include #include 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 found=0 ; int begin=10000000 ; // 10 million int end=20000000 ; // 20 million int first, second ; for(int i=begin ; i < end ; i++) { if ( isPrime(i) && isPrime(i+2) ) { found = 1 ; first = i ; second = i+2 ; } } if (found) { printf("Yes, there are twin primes between %d and %d.\n", begin, end) ; printf("One example is %d and %d.\n", first, second) ; } else { printf("No, there are no twin primes between %d and %d.\n", begin, end) ; } return 0 ; }