Project Euler Problems 1 to 100 - Problem 5: Smallest multiple

Tell us what’s happening:
When I execute my code as below, I got the message:
// running tests

smallestMult(20) should return 232792560.

// tests completed

// console output

Potential infinite loop detected on line 4. Tests may be failing because of this.

Potential infinite loop detected on line 4. Tests may be failing because of this.

But when I execute my code on my machine (my PC), the output of smallestMult(20) is 232792560 (same with the answer of problem 5).
I don’t know where my wrong.

Your code so far

function smallestMult(n) {
    let smallMul = n;
    let isTrue = false;
    while (!isTrue)
    {
      let count = 0;
      for(let i = 1; i <= n; i++){
        if(smallMul % i == 0){
          count++;
        }
      }
      if(count == n){
        isTrue = true;
      }
      else{
        smallMul++;
        count = 0;
      }
    }
    return smallMul;
  }

smallestMult(20);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36

Challenge: Project Euler Problems 1 to 100 - Problem 5: Smallest multiple

Link to the challenge:

Let’s apply conventional formatting:

function smallestMult(n) {
  let smallMul = n;
  let isTrue = false;

  while (!isTrue) {
    let count = 0;
    for (let i = 1; i <= n; i++) {
      if (smallMul % i === 0) {
        count++;
      }
    }
    if (count === n) {
      isTrue = true;
    } else {
      smallMul++;
      count = 0;
    }
  }
  return smallMul;
}

smallestMult(20);

Usually, when you see “Potential infinite loop”, you either a) have an infinite loop or b) your loop is way too slow.

In this case, your problem is smalMul++ makes your loop take way too long. How could you make that part go faster?

Thank for your support.
Unfortunately, I have used your recommend but still meet my issue.

What change did you make to fix this? Can you show your new code?

1 Like

Thanks for your support, my code makes my loop take way too long, Let me try another solution.

You don’t need to completely reinvent your approach. You need to fix that one line.