HDOJ 题目 3792 Twin Prime Conjecture(数学)
2014-09-09 23:21
423 查看
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.
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.
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
浙大计算机研究生保研复试上机考试-2011年
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
);
}
}
相关文章推荐
- hdoj3792 Twin Prime Conjecture
- hdu 3792 Twin Prime Conjecture 浙大复试上机题 水题
- HDU 3792 Twin Prime Conjecture 这题要用筛选法......
- HDU 3792Twin Prime Conjecture
- hdu 3792 Twin Prime Conjecture n之内的孪生素数个数
- hdu 3792 Twin Prime Conjecture
- 杭电 3792 Twin Prime Conjecture
- HDU 3792——Twin Prime Conjecture(数论)
- HDU 3792 Twin Prime Conjecture(素数筛法 + 树状数组)
- 筛素数——HDU 3792 Twin Prime Conjecture
- hdu 3792 Twin Prime Conjecture
- 素数 hdu 3792 Twin Prime Conjecture
- hdu 3792 Twin Prime Conjecture n之内的孪生素数个数
- HDU 3792 Twin Prime Conjecture
- 2011年浙大:Twin Prime Conjecture
- HDOJ题目1701ACMer(数学)
- HDOJ 题目2058The sum problem(数学)
- HDOJ 题目4112Break the Chocolate(数学)
- zju2011保研:Twin Prime Conjecture
- HDOJ, 杭电1108, 最小公倍数题目,简单的数学题。。。。