Twin Prime Conjecture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2249 Accepted Submission(s): 720
Problem Description
If we define dn as: dn = pn+1-pn, where pi is the i-th prime. It is easy to see that d1 = 1 and dn=even for n>1. Twin Prime Conjecture states that "There are infinite consecutive primes differing by 2".
Now given any positive integer N (< 10^5), you are supposed to count the number of twin primes which are no greater than N.
Now given any positive integer N (< 10^5), you are supposed to count the number of twin primes which are no greater than N.
Input
Your program must read test cases from standard input.
The input file consists of several test cases. Each case occupies a line which contains one integer N. The input is finished by a negative N.
The input file consists of several test cases. Each case occupies a line which contains one integer N. The input is finished by a negative N.
Output
For each test case, your program must output to standard output. Print in one line the number of twin primes which are no greater than N.
Sample Input
1 5 20 -2
Sample Output
0 1 4
Source
Recommend
notonlysuccess | We have carefully selected several similar problems for you:
3874
1568
3641
3430
1573
题意
twins prime:素数b-素数a==2,
求0-n有多少twins prime
ac代码
#include<stdio.h>
#include<string.h>
#include<math.h>
int a[100010],b[100010],cot;
void fun()
{
int i,j,w;
cot=1;
a[0]=2;
for(i=3;i<100010;i++)
{
w=1;
for(j=2;j<=sqrt(i);j++)
{
if(i%j==0)
{
w=0;
break;
}
}
if(w)
a[cot++]=i;
}
}
int main()
{
int i,j,k,t,n,ans=0;
fun();
t=2;
for(i=1;i<cot;i++)
{
//b[i]=b[i-1];
if(a[i]-t==2)
ans++;
t=a[i];
b[a[i]]=ans;
}
j=0;
for(i=0;i<100010;i++)
{
if(b[i]>j)
j=b[i];
b[i]=j;
}
while(scanf("%d",&n)!=EOF,n>=0)
{
printf("%d\n",b[n]);
}
}