public class PrimeDemo {
static int primeCount = 0;
public static void main(String[] args){
int num = 100;
System.out.println(num + "以内的素数:");
for(int i=3;i<=num;i++){
int tempCount = 0;
if(!isPrime(i,tempCount)){
++ primeCount;
System.out.print(i + "\t");
}
tempCount = 0;
}
System.out.println("\n100以内的素数总共有:" + primeCount + "个");
}
private static boolean isPrime(int a,int tempCount){
int sqrt = (int) Math.sqrt(a);
// for(int k=2;k<=a-1;k++){
for(int k=2;k<=sqrt;k++){
if(a%k == 0)
++ tempCount;
}
return (tempCount == 0) ? false : true;
}
}
求素数
最新推荐文章于 2022-10-23 20:43:17 发布