【程序 27 求素数】
题目:求 100 之内的素数
源码:
package com.homework.test;
/*
【程序 27 求素数】
题目:求 100 之内的素数
*/
public class Test27 {
public static void main(String [] args){
int count = 0;
for (int i=2; i<100; i++){
if (IsPrime(i)){
System.out.print(i + " ");
count++;
}
}
System.out.println("
" + "100以内一个有" + count + "个素数!");
}
public static boolean IsPrime(int n){
for (int i=2; i<=Math.sqrt(n); i++){
if (n % i == 0)
return false;
}
return true;
}
}