1007. 素数对猜想 (20)

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;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值