1007
让我们定义dn为:dn=p(n+1)−p(n),其中pi是第i个素数。显然有d1=1,且对于n>1有
dn是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。
现给定任意正整数N(<10^5
),请计算不超过N的满足猜想的素数对的个数。
输入格式:
输入在一行给出正整数N。
输出格式:
在一行中输出不超过N的满足猜想的素数对的个数。
输入样例:
20
输出样例:
4
package test1;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import java.util.Scanner;
public class PTA1007 {
public static void main (String [] args){
Scanner in=new Scanner(System.in);
int N=in.nextInt();
int count=2;
if(N<(10^5)) {
if (N == 3) {
System.out.print("1");
}
if (N >= 4) { //从四开始判断
for (int j = 4; j <= N; j++) {
if (S(j)) {//判断j是否为素数是则count加1
count++;
}
}
}
if (count % 2 == 0) {//判断素数的个数为奇数还是偶数
int t = 0;
t = count / 2;
System.out.print(t);
}
if (count % 2 == 1) {
int t = 0;
t = (count - 1) / 2;
System.out.print(t);
}
}
}
public static boolean S(int n){//判断是否为素数
for(int i=2;i<=Math.sqrt(n);i++){
if(n%i == 0)
return false;
}
return true;
}
}