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

类别:Java 点击:0 评论:0 推荐:

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://com.8s8s.com/it/it12265.htm