You are on page 1of 3

Twin Primes

Problem code: TGPA02 Submit All Submissions The students of IIST are developing their own satellite. The RF & Communications team isdesigning a new encryption technique. They require the usage of of Twin Primes in theircryptographic technique. (A twin prime pair is a pair of prime numbers having a difference of 2.)The more the number of pairs of Twin Primes the better their encryption technique would be andhappier their mentors in ISRO. They don't have any good programmers with them as of now, sothey have asked you to help them generate the count of twin primes between any two givennumbers.

Input Format
The first line will contain the number of test cases, t. For each test case, a single line contains 2 numbers, m and n seperated by a space. t<=20 1<=m<=n<=1000000000 n-m<=1000000

Output Format
The number of pairs of twin primes between m and n.
Sample Input: 2 1 10 100 200 Sample Output: 27

Date: 2010-02-24 Time limit: 1s Source limit:50000 Languages: C C++ 4.3.2

#include<iostream.h> #include<conio.h> #include<math.h> #include<time.h>

#include<stdio.h> int prime(long int a) { long int g,h,j; if(a==1) return 0; for(g=2;g<=sqrt(a);g++) { if(a%g==0) return 0; } return 1; } void main() { clock_t start, end; long int n,m,a,b,c,d,e,f,s=0; double r=0; clrscr(); cout<<"Enter the number of test cases less then 10"<<endl; cin>>a; for(n=1;n<=a;n++) { cout<<"\nEnter limits as 10 34 "<<endl; cin>>b>>c;

start = clock(); for(d=b;d<=(c-2);d++) { e=prime(d); f=prime(d+2); if(e==1 && f==1) s=s+1; } end=clock(); cout<<"\nThe time was: \n"<<((end - start) / CLK_TCK); r=r+(double)((end - start) / CLK_TCK); cout<<"\nTotal twin prime "<<s; s=0; } cout<<"\nTotal time = "<<r; getch(); }

You might also like