输出1-100间的素数并求和。

发表于:2007-07-04来源:作者:点击数: 标签:
public class ForPrime {

public static void main(String[] args) {
int sum = 0;
for (int i = 2; i < 100; i++) {
if (isPrime((long) i))
{
System.out.println(" isPrime = " + i);
sum = sum +i;
}
}
System.out.println("The sum is:"+sum);
}

static boolean isPrime(long num) {
double intSqrtOfNum = Math.sqrt((double) num);
int i = 0;
for (i = 2; i < intSqrtOfNum; i++) {
if (num % i == 0)
break;
}
if (i > intSqrtOfNum)
return true;
return false;
}
}


原文转自:http://www.ltesting.net