public void a() { for (int i = 2; i <= 100; i++) { int temp = (int) Math.sqrt(i); // 我把那个aqrt单独提出来,这样速度稍微快一点,虽然在100内变化不大,但如果是10000000内的素数呢? if (i <= 3) { System.out.println(i + " is a prime"); } else { for (int j = 2; j <= temp; j++) {// 把Math.sqrt(i)转换为int类形 if (i % j == 0) { break; } if (j >= temp) { System.out.println(i + " is a prime"); } } } } }
public void sushu(int n ,int m){ for (int i = n; i < m; i++) { int temp = (int)Math.sqrt(i); if(i<=3){ System.out.println(i); } for (int j = 2; j <= temp; j++) { if(i%j==0){ break; } if(j>=temp){ System.out.println(i); } } } }
原文地址:http://blog.csdn.net/zeb_perfect/article/details/43459471