简体   繁体   中英

Sum of first 1000 prime numbers

I have the below program where I am trying to find the sum of first 1000 prime numbers. In the code, what's the difference between solution 1, and 2? Why should I not put the count variable outside the if condition? I am obviously not getting the answer I need if I put the variable outside if, but I don't understand why its logically wrong. It could be a simple thing, but I am unable to figure it out. Experts, please help.

Solution 1:

public class SumOfPrimeNumbers {
public static void main(String[] args) {
    long result = 0;
    int number = 2;
    int count = 0;
    while (count < 1000) {
        if (checkPrime(number) == true) {
            result = result + number;
            count++;
        }
        number++;
    }
    System.out.println("The sum of first 1000 prime numbers is " + result);
}

public static boolean checkPrime(int number) {
    for (int i = 2; i < number; i++) {
        if (number % i == 0) {
            return false;
        }
    }
    return true;
}

}

Solution 2:

public class SumOfPrimeNumbers {    
public static void main(String[] args) {
    long result = 0;
    int number = 2;
    int count = 0;
    while (count < 1000) {
        if(checkPrime(number)==true)
        {
        result = result + number;                       
        }               
        count++; //The count variable here has been moved to outside the loop.
        number++;
    }       
    System.out.println("The sum of first 1000 prime numbers is "+ result);
}

public static boolean checkPrime(int number) {
    for (int i = 2; i < number; i++) {
        if (number % i == 0) {
            return false;
        }
    }       
    return true;
}

}

You should not check the return value of bool functions for equality to true : this line

if(checkPrime(number)==true)

is equivalent to

if(checkPrime(number))

Finally, the solution where the count is incremented outside of if counts non-primes together with primes, producing an obviously wrong result.

Here are a couple of points "for style" that you should consider:

  • Checking candidate divisors in checkPrime can stop when the candidate divisor is greater than the square root of the number
  • You can do much better if you store the primes that you've seen so far, and checking divisibility only by the numbers from the list of primes. When you are looking for the first 1000 primes this would hardly matter, but for larger numbers this could be significant.

The place where you increment count is pretty important. Your first code chunk adds up the first 1000 primes, while the second one adds up all the primes less than 1000.

In your solution 2, count is incremented EVERY time through the loop, regardless of the result of your prime test. So it is not counting primes, but counting iterations through the loop. As a result, you'll check 1,000 consecutive numbers, not consecutive primes (which involves going through a lot more than 1,000 numbers to accumulate).

In addition to what others have pointed out, your check for prime can be made a little more efficient by:

public static boolean checkPrime(int number) {
    int s = Math.ceil(Math.sqrt(number));

    for (int i = 2; i <= s; i++) {
        if ((number % i) == 0) {
            return false;
        }
    }       
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM