Project euler # 10, java, correct for small numbers

* disclaimer when I say: "I have confirmed that this is the correct result", please interpret this as I have checked my decision against the answer according to WolframAlpha, which I find to be fairly accurate.

* to find the sum of all primes less than or equal to 2,000,000 (two million)

*, my code will give the correct result when my range of tested values ​​is approximately less than or equal to

I do not give the correct result as soon as the test input becomes more than approximately 1,300,000; my output will be disabled ...

test input: ---- 199.999 test output: --- 709 600 813 correct result: 1 709 600 813

test input: ---- 799.999 test output: --- 24,465,663,438 correct result: 24,465,663,438

test input: ---- 1,249,999 test output: --- 57 759 511 224 correct result: 57 759 511 224

test input: ---- 1,499,999 test output: --- 82 075 943 263 correct result: 82 074 443 256

test input: ---- 1 999 999 test output: --- --- --- --- --- --- --- --- --- --- --- --- - - --- --- --- --- - - the correct result: 142 913 828 925

test input: ---- 49,999,999 test output: --- 72 619 598 630 294 correct result: 72 619 548 630 277

* my code, what happens, why does it work for small resources? I even used long, not int ...

long n = 3;
long i = 2;
long prime = 0;
long sum = 0;
while (n <= 1999999) {
  while (i <= Math.sqrt(n)) {    // since a number can only be divisible by all
                            // numbers
                            // less than or equal to its square roots, we only
                            // check from i up through n square root!
    if (n % i != 0) {       // saves computation time
      i += 2;               // if there a remainder, increment i and check again
    } else {
      i = 3;                // i doesn't need to go back to 2, because n+=2 means we'll
                            // only ever be checking odd numbers
      n += 2;               // makes it so we only check odd numbers
    }
  }                         // if there not a remainder before i = n (meaning all numbers from 0
                            // to n were relatively prime) then move on
  prime = n;                // set the current prime to what that number n was
  sum = sum + prime;
  i = 3;                    // re-initialize i to 3
  n += 2;                   // increment n by 2 so that we can check the next odd number

}
System.out.println(sum+2); // adding 2 because we skip it at beginning

help me please:)

+6
1

, , , , . , :

while (n <= 1999999) {

:

 while (i <= Math.sqrt(n)) {

(n += 2;) . - , , , .

, 1,999,999, 1 999 999, 2000 000 , , , 142 913 828 922, 2000,003 , 142 915 828 925.

, continue , :

public static final long primeSum(final long maximum) {
    if (maximum < 2L) return 0L;
    long sum = 2L;

    // Put a label here so that we can skip to the next outer loop iteration.
    outerloop:
    for (long possPrime = 3L; possPrime <= maximum; possPrime += 2L) {
        for (long possDivisor = 3L; possDivisor*possDivisor <= possPrime; possDivisor += 2L) {
            // If we find a divisor, continue with the next outer loop iteration.
            if (possPrime % possDivisor == 0L) continue outerloop;
        }
        // This possible prime passed all tests, so it an actual prime.
        sum += possPrime;
    }

    return sum;
}
+7

Source: https://habr.com/ru/post/1015352/


All Articles