码迷,mamicode.com
首页 > 其他好文 > 详细

1007. 素数对猜想 (20)

时间:2018-02-25 13:09:52      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:std   bsp   print   break   存在   计算   gpo   输入   str   

让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数。显然有 d1=1 且对于n>1有 dn 是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。

现给定任意正整数N (< 105),请计算不超过N的满足猜想的素数对的个数。

输入格式:每个测试输入包含1个测试用例,给出正整数N。

输出格式:每个测试用例的输出占一行,不超过N的满足猜想的素数对的个数。

输入样例:

20

输出样例:

4
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 4 
 5 int isPrime;     //素数标记
 6 int main()
 7 {
 8     int n,x,y=2,z=3;
 9     int i,j;
10     int cnt=0;
11     scanf("%d",&n);
12     for( x=4; x<=n; x++)
13     {
14         isPrime =1;
15         j = (int)sqrt(x)+1;  //没有开根号之前一直运行超时
16         for( i=2; i<j; i++)
17         {
18             if( x%i ==0)
19             {
20                 isPrime = 0;
21                 break;
22             }
23         }
24         if( isPrime )
25         {
26             y = z;
27             z = x;
28             if( z-y==2)
29                 cnt++;
30         }
31     }
32     printf("%d\n",cnt);
33     return 0;
34 }

 

1007. 素数对猜想 (20)

标签:std   bsp   print   break   存在   计算   gpo   输入   str   

原文地址:https://www.cnblogs.com/yuxiaoba/p/8468958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!