求一百以内的素数 public class FindPrime { /** * @param args */ public static void main(String[] a...
求一百以内的素数
public class FindPrime {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = 100;
String s = "100以内的素数:";
for (int i = 1; i <= num; i++) {
int count = 0;
for (int j = 1; j <= (int) Math.sqrt(i); j++) {
if (i % j == 0)
count++;
}
if (count > 1)
s += "";
else
s += i + " ";
}
System.out.println(s);
}
//result
//100以内的素数:1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
}
学员评论