Bug? Getting different results in FCC suite vs codepen/local computer

Bug? Getting different results in FCC suite vs codepen/local computer
0

#1

Edit: Turns out there are no bugs here, I made a typo thanks gebulmer for figuring it out!

Hey guys, I was doing the Intermediate Algorithm Scripting: Sum All Primes challenge and having some problems with it. I wanted to do it anyways and coded it locally on my machine. I got it to work in the fcc page but the results are different than what I got. Its the same exact code. I pasted it into codepen.io as well and it gave me the same result as my local computer.

These are the output from the fcc page (the code passed all the tests):

New for: 10
12:55:47.637 VM358:42 upper limit: 3  (rounded down square root of num)
12:55:47.641 VM358:43 result: 17
12:55:47.654 VM358:41 

New for: 977
12:55:47.664 VM358:42 upper limit: 31  (rounded down square root of num)
12:55:47.675 VM358:43 result: 73156

These are the results from my local machine/codepen.io


New for: 10
test-3.html:22 upper limit: 3  (rounded down square root of num)
test-3.html:23 result: 17
test-3.html:21 

New for: 997
test-3.html:22 upper limit: 31  (rounded down square root of num)
test-3.html:23 result: 76127

Ill attach screen shots below.




This is adding all the prime numbers up on google, pulled from a list of prime numbers on another website.


#2

The difference between 73156 and 76127 is precisely the sum of the primes between 977 and 1000

I’m not sure that’s a coincidence, but I need to think about about what’s going on

For the record it’s easier for us if you post the code itself rather than the screenshot, so we can copy it easily into our favourite text editor/linter/etc and play around with it


#3

Okay gotcha, I’ll do that from now on. Heres the code I am using.

function sumPrimes(num) {
  let primeSum = 0;
  let primeNums = [];
  let upper = Math.floor(Math.sqrt(num));
  for (let i = 2; i <= num; i++){
    primeNums.push(i);
  }

  for (let i = 2; i <= upper; i++){
    for (let j = i * i; j <= num; j += i){
      //console.log("deleted["+i+"]: " + primeNums[j-2]);
      primeNums[j-2] = 0;
    }
  }

  for (let i = 0; i < primeNums.length; i++){
    primeSum += primeNums[i];
  }

  console.log("\n\nNew for: " + num);
  console.log("upper limit: " + upper + "  (rounded down square root of num)");
  console.log("result: " + primeSum);

  return primeSum;
}

sumPrimes(10);
sumPrimes(997);

#4

Just noticed in your screenshot you’ve included the primes 983, 991, and 997

Not sure how you’ve ran your code locally and gotten a different answer, as when I run your code locally it gets the correct answer, (73156)

Edit: I see why from your codepen screenshot too, you had a brain fart (we all do it sometimes so no worries) and put 997 as the argument instead of 977


#5

Huh, interesting. Did you try running it on codepen or repl? Im getting 76127 on both as well.


#6

Check your argument again, it should be 977 not 997


#7

Oh my you are right haha, I can’t believe I didn’t notice that the whole time! thanks


#8

Haha no worries, think I did exactly the same when I did this question, silly brains tricking us because of the double numbers probably!


#9

btw great work implementing the sieve of erathosthenes!

Later on if you get to the project euler questions, you may want to hold on to your sieve code, it’ll help a lot

And if you’re feeling bored and want to improve your sieve, consider reading about wheel factorisation, you can combine it with your sieve to improve its performance by reducing how many candidates you have to test!