1007. 素数对猜想 (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数。显然有 d1=1 且对于n>1有 dn 是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。
现给定任意正整数N (< 105),请计算不超过N的满足猜想的素数对的个数。
输入格式:每个测试输入包含1个测试用例,给出正整数N。
输出格式:每个测试用例的输出占一行,不超过N的满足猜想的素数对的个数。
输入样例:20输出样例:
4
package Basic1007;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
long time1=System.currentTimeMillis();
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int cnt = 0;
int x = 2, y = 2;
for (int i = 2; i <= num; i++) {
if (judge(i) == 1) {
x = y;
y = i;
if (y - x == 2) {
cnt++;
}
}
}
System.out.println(cnt);
long time2=System.currentTimeMillis();
System.out.println(time2-time1);
}
public static int judge(int x) {
for (int i = 2; i < x; i++) {
if (x % i == 0) {
return 0;
}
}
return 1;
}
}