1 package text;
2
3 public class TestPrimeNumber {
4 public static void main(String[] args){
5 boolean flag = false;
6 long start = System.currentTimeMillis();
7 l:for(int i = 2; i < 100000; i++){//从0遍历到100
8 for(int j = 2; j <= Math.sqrt(i); j++){//127,判断i是否为质数
9 if(i % j == 0){
10 continue l;
11 }
12 }
13 System.out.println(i);
14
15 }
16 long end = System.currentTimeMillis();//17097
17 System.out.println("所花费的时间为:" + (end - start));
18 }
19 }
1 package text;
2
3 public class TestPrimeNumber {
4 public static void main(String[] args){
5 boolean flag = false;
6 long start = System.currentTimeMillis();
7 for(int i = 2; i < 100000; i++){//从0遍历到100
8 for(int j = 2; j <= Math.sqrt(i); j++){//127,判断i是否为质数
9 if(i % j == 0){
10 flag = true;
11 break;//1635
12 }
13 }
14 if(!flag){
15 System.out.println(i);
16 }
17 flag = false;//改回判断旗帜
18 }
19 long end = System.currentTimeMillis();//17097
20 System.out.println("所花费的时间为:" + (end - start));
21 }
22 }
23 /*
24 * 循环内加添加 break
25 * 判断改变为 Math.sqrt(i)
26 */