鑒於我總是忘記這個知識點,所以我專門出一篇這個隨筆,來幫助記憶
首先,得明白一個點,1既不是質數也不是合數
class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N=sc.nextInt(); for (int i = 2; i <N ; i++) { boolean b=true; for (int j = 2; j <=Math.sqrt(i) ; j++) { if (i%j==0){ b=false; } } if (b){ System.out.println(i); } } } }
此代碼已經過驗證,可放心使用