Need Help SCM task

Need Help SCM task
0

#1

Hi,

So i have been stuck on SCM - https://www.freecodecamp.org/challenges/smallest-common-multiple for some time my algo is like this -

1)Take the two number and sort then in ascending or descending order
2)Take a new array and insert all values between the 2 ranges
3)Then multiply each number in array(get their multiples) and find the first number which is common in all multiples of number-

So for 1,2,3,4,5

I know

1 like 1,2,3,4,5,6,7,8…
2 like 2,4,6,8,10,12 etc
3like 3,6,9, etc
4 like 4,8,12,16, etc
5 like 5,10,15 etc and its 60 which is first lcm …

But i do not know how to do this in program ,and create these loops …

I can only think in a simple way and not good with any complex thing …can some one please guide -

My jsfiddle link - https://jsfiddle.net/jinisner/y6dvLe50/ , thanks



function smallestCommons(arr) {
    var test = [];
    var x;
    
    arr.sort(function(a,b){
       return b-a; 
    });
   
    for(var i = arr[0]; i >= arr[1];i--){
        test.push(i);
    }
    
    console.log(test);
    
   
    
 //  console.log(test); 
  return arr;
    
}


smallestCommons([1,5]);

#2

Any help will be appreciated i have gone through some solutions here and i am not able to understand any thing really …

Regards


#3

In the hint section of the challenge there is a link to the wikipedia article for LCM. If you’ve read through the article you would have noticed this paragraph:

As a general computational algorithm, the above is quite inefficient. One would never want to implement it in software: it takes too many steps, and requires too much storage space. A far more efficient numerical algorithm can be obtained simply by using Euclid’s algorithm to compute the gcd first, and then obtaining the lcm by division.

This is place to start.


#4

@nr-mihaylov - thanks, so instead of looking for my own solution i should follow the Euclid algorithm ?