Problem with Sum All Primes Intermediate challenge

Problem with Sum All Primes Intermediate challenge
0.0 0

#1

I wrote the code as per:

called the function sumPrimes(977);
got the correct result of 73156
But when I ran the tests, it will not pass me even though my code works and I get the correct results!!

Why?

My code is:

function sumPrimes(num) {
var primenum = [2];
for (var i = 3; i < 1000; i++) {
// lets see if i is a prime number
var match = false;
for (var j = (i-1); j > 1; j--){
for (var k = 2; k < i; k++) {
if (j * k == i) {
match = true;

}

}

}

if (match == false) {
  primenum.push(i)
}
}
console.log("the prime array is: ", primenum);
    
    for (var m = 0; m < primenum.length; m++) {
   if (num < primenum[m]) {
       var sum = 0;
     for (var n = (m -1); n >= 0; n--)  {
         sum = sum + primenum[n];
               
     }
       console.log("the sum is: ", sum);
       num = sum;
       console.log("the num is: ", num);
       return num;
   }     
         
    }

}

sumPrimes(977);

#2

https://forum.freecodecamp.org/t/free-code-camp-infinite-loop-protection/19550


#3

I still do not see the point! My program gets the correct result. But FCC does not accept it.

What am I supposed to do!


#4

Read the link in the above post and follow the instructions to remove the infinite loop protection


#5

Hi,
Just an observation. The first big loop calculates all the primes up to 1000.

What if you calculated all the primes up to (and possibly including) num? Then just add those up to get sum. Or even better, just add them on the fly as you calculate those Primes


#6

AlHazen1,

1) mark me if I am wrong, but when I run the tests, I do not see the infinite loop error message “Error: Potential infinite loop at line X” that you said I am triggering. Instead, all I get is:
// running test
sumPrimes(977) should return 73156.
// tests completed

Therefore I wonder whether or not my program is triggering the infinite loop shutdown as you indicated.

2) I am concerned that your suggestion to rewrite my code so that it executes more quickly will not do much, because num is 977 and the difference between 977 and 1000 is small and therefore deminimus. As a result, I am concerned that the savings in calculations by dropping 1000 to 977 in my code would be insignificant and not be enough to avoid any time out issues.


#7

I know that the output box is being worked on because it isn’t printing everything that it should. Just because the infinite loop message isn’t printing to that box right now doesn’t mean it isn’t being triggered.

By the way, all of these challenges can be solved with functions that are efficient enough not to trigger the infinite loop protection.


#8

Ariel Leslie,

  1. If the infinite loop message does not display, How do you know my code is triggering the infinite loop shutdown?

  2. I put the comment " //noprotect" on every other line of my code and this has not changed the results when I run the tests. This may also be additional evidence that my code is not triggering the infinite loop shutdown.


#9

It will. As I said, it’s not working correctly right now and is being fixed.

It’s possible that the //noprotect isn’t working. You can open a GitHub Issue for this if there isn’t one already. I can get this test to pass when the function returns the correct value, so it doesn’t always fail.


#10

As per your request, I started a GitHub issue on this.
It is called Algorithm Scripting challenge #17374


#11

I tested you code in repl.it, and refactoring would be a really good idea. Yes you code works, but it takes a very long time to execute, in fact it takes MUCH longer to execute sumPrimes(97) than my code takes to execute sumPrimes(9777).

Try, creating an array filled with the numbers 1 to num
then use array.prototype.forEach to iterate the array you just created, use a while loop inside it to find your primes… (hint: Math.sqrt and % modulus come in handy here)

the tl;dnr definitely refactor your code, the whole challenge will complete in less than a second.

if you need more help I can usually be found on the unofficial freecodecamp discord chat server


#12

Hi @Freds,
It was really refreshing to see that you bothered to come up with your own home-brewed solution instead of just pinching an algorithm from Stack Exchange or someone else’s CodePen like so many do.

At some point it would benefit you to revisit this challenge with a mind to refactor for speed as you acquire new coding skills.

I did go to the “new” FCC site and ran a fairly fast Sum Primes solution several times only to have it fail once without any indication why. So there may indeed be something a bit off about the tests on that challenge.


#13

Alhazen,

I’m glad you recognized my efforts! Thanks!

Pahosler,

I did modify my code to include the square root upper limit on one of the two nested loops, but still got the same error.

see the code at:
https://codepen.io/smithfred0439/pen/wXamLY?editors=1111

where I included the upper limit:

y = Math.round(Math.sqrt(i));

I also included numerous:

//noprotect

as per the article, but FCC consistently keeps failing me for the last test of num = 977 despite the fact that I get the correct result of 73156, and in doing so, FCC fails to display an error message that my code is taking too long.

es


#14

Hi Freds,
The square root thing might save you a lot of unneeded calculation. I knew I didn’t need to calculate all possible factors but it took me a while to grasp that the cut off was the square root. After analyzing a ton of console.log output I came to a conclusion over time how to best calculate primes mathematically.

  1. Testing all numbers less than number to see if it was a factor - no brainer
  2. Testing only prime numbers less than number to see if it was a factor - aha moment
  3. Testing all prime numbers less than square root of number to see if it was a factor - mixed results

Obviously #3 is a hellalot less calculations than #1.

By no means the best solutions but some that show the different directions I went with this before I arrive at something I liked. Maybe they’ll be of some help to you.
SumAllPrimes

Good luck to you.


#15

Question:
Is this supposed to be part of the challenge (that is to refactor your code so it consumes less time) in order to pass, or is this an unintended consequence of this challenge?

I happened to skip to the next challenge called “smallest common multiple”, and am running into the same problem where I get the correct answer when I run it on my own, but the FCC program fails me in the last 2 tests without indicating that I took too much time.

This intermediate scripting was sooo easy until now that I ran into this infinite loop issue with FCC’s program.


#16

Try Chrome if you are using Firefox, well it worked for many others. I saw this as one of a comment in their GitHub issues list.


#17

I just tried in Chrome browser and got the same result that I got when I used Firefox.