Problem 5: Smallest multiplePassed

Tell us what’s happening:
Describe your issue in detail here.

I am trying to solve Euler problem 5, and the code works locally.
It solves the challenge locally but when trying to submit my solution to fcc the last test breaks because of a potential infinite loop.

locally the last test runs in about 1 to 4 seconds.

Your code so far

function smallestMult(n) {

  let numbers = [];
  let i = 1;
  outerloop: while (numbers.length <= n) {

    for (let j = 1; j <= n; j++) {
      
      if(numbers.length == n){
        break outerloop;
      }
      
      if (i % j == 0 && i != j) {
        numbers.push(i)
      } else {
        i++
        numbers = []
      }
    }
  
  }
  console.log(numbers[0])
  return numbers[0]
}


Your browser information:

User Agent is: Chrome
Challenge: Problem 5: Smallest multiple

Link to the challenge:

From my experience anything taking longer than one second for single case may have problems with passing on site.

If you want small tip, without making many changes - consider what can be done to ensure each checked number is divisible by the largest required divisor in a given case.

2 Likes

consider what can be done to ensure each checked number is divisible by the largest required divisor in a given case.

I don’t really understand what you mean by this. Can you give me an explanation without spoiling me the answer?

I’ve tried to write explanation, but I couldn’t be sure it wasn’t spoiling something. Instead I’ll try to formulate it as a bit expanded questions.

First thing - what in each test case is the largest divisor, by which number have to be divisible?
How it can be ensured each candidate number is divisible by that largest divisor?

I think I get what you mean thanks.

One simple change and it runs like a charm thanks @sanity

1 Like

Now that your code runs, this is a problem where looking at how others have solved it can be very helpful.

I could but in project Eulers philosophy and rules they discourage sharing your solution to the problem.

You can read about it here:
https://projecteuler.info/about

So I am going to respect those rules.

I am well aware of their stance. They take an even more extreme stance than me. Personally, I think it is important to solve the problem for yourself, but you are cheating yourself out of the opportunity to learn as a developer if you refuse to look at alternative approaches to solving the problem once you have created a solution yourself.

Note by the way, that their stance does not say anything about people who have all solved the problem looking at each other’s solutions. They are trying to avoid spoilers.

A strict policy of only using knowledege you created yourself is seen as a bad thing for professional developers.

In this case, you should look to number theory to greatly improve the performance of your algorithm. There are mathematical theorems that you can leverage to change the complexity class of this algorithm.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.