package come;
/*
* 求1-100所有素数
*/
public class Test2 {
public static void main(String[] args) {
int m = 1;
int n = 1000;
int count = 0;
for (int x = m; x < n; x++) {
if (getsushu(x)) {
count++;
System.out.println(x + " ");
if (count % 10 == 0) {
System.out.println();
}
}
}
System.out.println("在" + m + "到" + n + "之间有" + count + "个素数");
}
private static boolean getsushu(int n) {
boolean flag = false;
if (n == 1) {
flag = false;
}else if (n == 2 || n == 3) {
flag = true;
} else {
for (int i = 2; i <= Math.sqrt(n); i++) {
if ((n % i) == 0 || n == 1) {
flag = false;
break;
} else {
flag = true;
}
}
}
return flag;
}
}
|
|