Java程序:
public class Test {
public static void main(String[] args) {
int i, count = 0;
for(i=2; i<=100; i++){
if(isPrimeNumber(i) == true){
count++;
System.out.printf("%6d", i);
if(count%5 == 0)
System.out.println();
}
}
}
//判断一个数是否是素数,若是,返回true,否则返回false
public static boolean isPrimeNumber(int num){
int k = (int) Math.sqrt(num);
if(num == 2)
return true;
for(int i=2; i<=k; i++)
if(num%i == 0)
return false;
return true;
}
}
运行测试:
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