PTA试题1----素数对问题

import java.util.Scanner;


public class PrimePair {
    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    scan.close();
    if(N <= 100000) {
    int num1 = 2;  //用来记录第i个素数的值
    int num =0; //记录第i-1个素数的值
    int record = 0;  //记录符合条件的素数对
    for(int i = 2; i <= N; i++) {   //遍历寻找N以内的素数
    boolean bool = true;
    for(int j = 2; j * j <= i; j++) {
    if(i % j == 0) {
    bool = false;
    break;
    }
    }
    if(bool) {
    num = num1;
    num1 = i;
    if(num1 - num == 2) {
    record++;
    }
    }
    }
    System.out.println(record);
    }
    }
}

相关文章:

  • 2021-04-30
  • 2021-10-11
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-29
  • 2021-09-30
  • 2022-12-23
  • 2021-05-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案