Twin primes are pairs of primes that are separated by 2 exam
Twin primes are pairs of primes that are separated by 2, example:
3/5 are twin primes, 5/7 are twin, and 11/13 are also twin primes.
Write a c program that can identify twin primes and ignoring the first 99,999 pairs, what is the 100,000 twin prime? (hint: you should get the values: 18409199, 18409201)
In addition, you need to have the program determine how long it takes to calculate this pair. A reasonable target is approx. 8 seconds or less. Display the time in milli-seconds.
Example:
Starting….
18409199, 18409201
Completed in: 7.438 seconds
I suggest using the ‘C’ library function: clock() which returns a time count in milliseconds. If you perform the following sequence, you will be able to determine how long the program takes to complete:
int time_beg, time_end, time_elapsed;
time_beg = clock();
Do_Prime_Number_Calc();
time_end = clock();
time_elapsed = time_end – time_beg;
Solution
PROGRAM CODE:
/*
* TwinPrime.c
*
* Created on: 22-Jan-2017
* Author: kasturi
*/
#include <stdio.h>
#include <time.h>
int checkPrime(long n)
{
for(long i=2; i<=n/2; ++i)
{
// condition for nonprime number
if(n%i==0)
{
return 0;
}
}
return 1;
}
void Do_Prime_Number_Calc()
{
long counter = 0;
long number = 3;
int prime1 = checkPrime(number);
while(counter!=100001)
{
number = number+2;
int prime2 = checkPrime(number);
if(prime1==1 && prime2==1)
{
counter++;
prime1 = 1;
}else if(prime1 == 1 && prime2 == 0)
{
prime1 = 0;
prime2 = 0;
number = number+2;
prime1 = checkPrime(number);
}
}
printf(\"Pair: %ld & %ld\", number-4, number-2);
}
int main(void) {
clock_t start_t, end_t, total_t;
start_t = clock();
Do_Prime_Number_Calc();
end_t = clock();
total_t = end_t - start_t;
printf(\"Time elapsed: %ld\", total_t/CLOCKS_PER_SEC);
return 0;
}
The above program will have to run for a considerable time to give the output. My machine has less configuration and hence will take a lot of time. This will give result sooner in a high configuration machine.

