Smallest Common Multiple - never been so stuck in my life

Smallest Common Multiple - never been so stuck in my life
0

#1

I am pretty stuck here.

I understand what the problem is asking for: return a single number that is divisible by all the numbers within a range. So if the range is 1 to 7 then the number we want to return should be divisible by 1,2,3,4,5,6, and 7.

After reading up on Euclidean math (which I did not understand) and this little nifty mathematical trick for finding LCM, I am NO closer to understanding how to code for this.

So I caved and looked at the Basic Solution. But I am at a loss as to what the do...while loop is doing. Especially this line: quot = newArr[0] * loop * newArr[1];

I’ve read the explanation several times and it does not shed any light, at last not for me.

Can anyone help me understand they WHY behind the Basic Solution?

Thank you.

Your code so far


function smallestCommons(arr) {
  return arr;
}


smallestCommons([1,5]);

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple


#2

Do not worry if you do not understand the solutions in the guide. The most important thing is you get the solution yourself.

However, if you have been stuck for a while, it is valid to ask for a hint. And I recommend that you solve by parts. :thinking: Is it not easier to obtain the LCM of just two numbers? Once you get this, think of the array not as a whole but as a queue.

PD: I just looked at the basic solution of the guide, and I do not understand it. It is not the most intiutive.


#3

Hmm - that’s an interesting way to look at it. Will take a look and get back. Thanks!


#4

Hmm, how funny is that: this could have been my post cause I am currently in a similar situation. I produced a monster-code that ist working with arguments:
[1,5]
but not with [4,5].

I think I misunderstand the solution so what I will try next ist to replay the algorithm just with a pencil and a piece of paper.


#5

Don’t get frustrated. Some of these are hard. I remember that one being difficult, and I’m pretty good at these things. Some of those problems are pretty easy and some are real ball busters. And this (iirc) is one that you can even get code that works but will not pass because it is not efficient enough.

Just think it through.

Start with a brute force solution. You’ve got to check number by number and see if it is a multiple of all the numbers in the range you are given.

Then, how would you make it more efficient. Do you really need to check every number? Think about how multiples work. [hint, hint]