Twin Prime Conjecture
Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1425Accepted Submission(s): 432
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
Sample Output
Source
孪生素数即 两个素数之差为2
问n之内的孪生素数对数
思路: 首先打印素数表 不用记录素数 而是用vis标记这个数是不是素数 之后如果vis[i]以及vis[i-2]都没标记 那么对数加1即可
#include<stdio.h>
#include<math.h>
#include<string.h>
int vis[100000+100];
int prime[80000],c;
int a[100000+10];
void get_prime()
{
int i,j,n,m;
c=0;
n=100000;
m=(int)sqrt(n+0.5);
memset(vis,0,sizeof(vis));
for(i=2;i<=m;i++) if(!vis[i])
{
for(j=i*i;j<=n;j+=i) vis[j]=1;//vis的不是素数
}
// for(j=2;j<=n;j++) if(!vis[j])
// prime[c++]=j;
}
int main()
{
int i,j,n,max;
get_prime();
n=0;
a[1]=0;a[0]=0;a[2]=0;a[3]=0;a[4]=0;
for(i=4;i<=100000;i++)
{
// printf("%d %d\n",!vis[i],!vis[i-2]);
if(!vis[i]&&!vis[i-2])
{
n++;
}
a[i]=n;
}
while(i<=100000)
{
a[i++]=n;
}
while(scanf("%d",&n)!=EOF)
{
if(n<0) break;
printf("%d\n",a[n]);
}
return 0;
}
分享到:
相关推荐
Largest prime factor Everybody knows any number can be combined by the prime number. Now, your task is telling me what position of the largest prime factor. The position of prime 2 is 1, prime 3 is 2,...
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
Hdu 3333解题报告 题意描述: 给你n个数现在要你求在k个区间上[ai, bi]的不相同的数之和各是多少. N,000; k,000; 显然,这题不能用暴力来做。 这题我们选择用线段数来做。
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu2101AC代码
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
Least Common Multiple Problem Description The least common multiple (LCM) of a set of positive integers is the smallest ...注意:要考虑到输入的输入的n个数中的0,有0的要去掉0求其他数的最小公倍数。 代码:
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
hdu 1166线段树代码
HDU最全ac代码
自己做的HDU ACM已经AC的题目
hdu动态规划算法集锦
这个是杭电hdu的一个分类,新手们可以按照这个来刷题!