代码如下:
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int N = input.nextInt(); boolean flag; ArrayList<Integer> arr = new ArrayList<Integer>(); for(int i = 2;i<=N;i++){ flag = false; for(int j=2;j<=Math.sqrt(i);j++){ if(i%j==0){ flag=true; break; } } if(flag==false){ arr.add(i); } } int count = 0; for(int i = 0;i<arr.size()-1;i++){ if((int)Math.abs(arr.get(i+1)-arr.get(i))==2){ count++; } } System.out.println(count); } }