Twin Prime Conjecture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2659    Accepted Submission(s): 906

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
求区间素数,注意打表
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
bool isPrime[MAXN];
int cnt[MAXN];
int main()
{
int n;
memset(isPrime,true,sizeof(isPrime));
for(int i=;i<MAXN;i++)
{
if(isPrime[i])
{
for(int j=i+i;j<MAXN;j+=i)
isPrime[j]=false;
}
}
cnt[]=;
int pre=;
int num=;
for(int i=;i<MAXN;i++)
{
if(isPrime[i])
{
if(i-pre==)
{
num++;
}
pre=i;
}
cnt[i]=num;
}
while(scanf("%d",&n)!=EOF&&n>=)
{
printf("%d\n",cnt[n]);
}
return ;
}

最新文章

  1. 分布式任务&amp;分布式锁(li)
  2. MyEclipse 2014跟2015破解
  3. 各种类型转换为字符串类型(ToString())
  4. Arcgis Server 10.2默认服务端口号修改方法
  5. 工具fiddler学习
  6. Oracle Database 11g Express Edition学习笔记
  7. github中non-fast-forward错误的解决
  8. 数据表列名与数据库关键字冲突,在Hibernate下的解决办法
  9. 回传值(代理、通知、block)
  10. Django中的权限系统
  11. PAGELATCH_x 等待--转载
  12. 77. Combinations(medium, backtrack, 重要, 弄了1小时)
  13. NetCore版RPC框架NewLife.ApiServer
  14. Hadoop-HA机制工作原理
  15. 完成一个java项目需要的一些基础
  16. React Native 入门基础知识总结
  17. myeclipse debug模式 报错source not found
  18. VGA原理
  19. php数组方法
  20. 〖Android〗CyanogenMod同步错误的解决

热门文章

  1. JS中的call_user_func封装
  2. C# 杀掉后台进程
  3. StringBuilder的append、StringBuffer的append和String str = &quot;a&quot;+&quot;b&quot;的区别?
  4. 基于红帽5裁剪一个简单的Linux
  5. 偏函数应用 pfa
  6. pgsql 数据类型
  7. Git 重写历史 filter-branch
  8. Java for LeetCode 089 Gray Code
  9. re.sub用法
  10. client , offset , scroll 系列 及百度导航栏案例