暴力遍历判断质数即可:
import java.util.*; public class Main { public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int r = 0; for(int i = 2; i < N; ++i){ if(f(i)) r++; } System.out.println(r); } static boolean f(int n){ //遍历判断质数 if(n <= 1) return false; if(n == 2) return true; for(int i = 2; i < n; ++i){ if(n%i == 0) return false; } return true; } }