第一种:双重for循环 使除数与被除数个个计算,效率极低public void test1(int n){
long start = System.currenttimeMillis(); //取开始时间
int num=0;
boolean sign;
for(int i=2;i
if(i % 2 == 0 && i != 2 ) continue; //偶数和1排除
sign=true;
for (int j=2;j
if(i%j==0){
sign=false;
break;
}
}
if (sign){
num++;
/* System.out.println(""+i);*/
}
}
System.out.println(n+"以内的素数有"+num+"个");
long end = System.currentTimeMillis();
System.out.println("The time cost is " + (end - start));
System.out.println("");
}
第二种:主要考虑2 ~ i/2之间的数 ,效率比第一种提高一半public void test2(int n){
long start = System.c