Twin PrimesProblem code: TGPA02 |
The students of IIST are developing their own satellite. The RF & Communications team is designing a new encryption technique. They require the usage of of Twin Primes in their cryptographic 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 and happier their mentors in ISRO. They don't have any good programmers with them as of now, so they have asked you to help them generate the count of twin primes between any two given numbers.
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: 2 7
| Author: | theta |
| Date Added: | 24-02-2010 |
| Time Limit: | 1 sec |
| Source Limit: | 50000 Bytes |
| Languages: | C, CPP 4.3.2 |
Comments

Fetching successful submissions

Output Format The number of
Output Format
The number of pairs of twin primes between m and n.
should m and n also be included (if they are twin primes)?
#include<iostream.h>#include<
#include<math.h>
using namespace std;
{
int j,flag=1;
for(j=2;j<n;j++)
{
if((n%j)==0)
{
flag=0;
break;
}
}
return(flag);
}
main()
{
int m[20],n[20],p,q,i,k,t,count;
cin>>t;
for(i=0;i<t;i++)
cin>>m[i]>>n[i];
for(k=0;k<t;k++)
{
count=0;
p=m[k];
q=n[k];
for(i=p;i<=q;i++)
{
if(prime(i)&&prime(i+2))
{
count=count+1;
}
}
cout<<count<<"n";
}
}
Posting your code here during
Posting your code here during the contest will lead to diqualification.