 # Stuck on Smallest Common Multiple

Stuck on Smallest Common Multiple
0

#1

Hello all,

I’m am currently working the Smallest Common Multiple algorithm challenge.
I am able to get the least common multiple - by coding out the formula mentioned in the Wikipedia link under Hints.

The part I can’t seem to wrap my head around (for longer than I’d like to admit haha)…
Is how to check if the value is divisible by the range of the two arguments?
Making an array of the range -made sense to me, but I’m not sure what to do with it?

``````
function smallestCommons(arr) {
var min = Math.min(arr,arr);
var max = Math.max(arr,arr);
var range = [];

for(i = min; i < max; i++){
range.push(i);
}

return lcm(min, max);
}

function lcm(a,b){
return (a * b) / gcd(a,b);
}

function gcd(a,b){
if(!b){
return a;
}
return gcd(b, a % b);
}

smallestCommons([1,5]);
``````

I might be approaching this in the wrong way altogether - so any feedback is appreciated.

#2

Could really use help being put in the right direction!..Thanks .

#3

`lcm` takes 2 numbers and reduces them to 1 number.
You have an array of arguments and need to reduce them to single number.
Lets take sum function: `sum (a, b) => a + b`
We can reduce array `[1, 2, 3]` by applying sum to first two numbers, then sum the result and last number, which gives us 6.

Now imagine number array of arbitrary length - we can apply same scheme - take first 2 arguments, reduce them into 1 and take and reduce until we got final result.

`lcm` and `gcd` share same behavior as `sum` function.

or you can just slap lcm into array’s .reduce method, which does exactly same thing

more fancy math horrors:
good one: http://blog.ploeh.dk/2017/10/06/monoids/
more on subject: http://blog.ploeh.dk/2017/11/20/monoids-accumulate/
official one: https://en.wikipedia.org/wiki/Monoid

p.s. also you have error there:

``````var max = Math.max(arr,arr);
``````