Finding the Smallest Common Multiple in JavaScript (and Also in Ruby)

Daniel Andrew
The Startup
Published in
5 min readNov 19, 2020

Well this is the story, all about how my sums got flipped, turned upside down. And I’d like to take a minute, just sit right there, I’ll tell you how to work out Common multiples in JavaSript.

Yeah doesn’t work so well. However nor does JS when doing either of these compared to Ruby. Needless to say its good to know how to do it in both!

So then lets have a gander at JS and getting the smallest common multiple, shall we?

In this case that is finding the smallest number that all numbers within a given range is divisible by with no remainder. E.g with a range of numbers between 1 and 5, the smallest common multiple is 60. As 60 can be divided by 1,2,3,4 & 5 with no remainder.

Now thats out of the way lets look at the code to make this possible:

function smallestCommons(arr) {
let min = Math.min(...arr) let max = Math.max(...arr) let array = []
// Creating a full array of all values in the range for (min; min<=max; min++){ array.push(min) }
//Creating the function that .every will operate on const lowestCommon = (currentValue) => n % currentValue === 0; let common = false let n = max* (max-1)// Checking whether the first value for n is the lowestCommon Multiple common = array.every(lowestCommon)
//Checking for a true result from the array while (common === false){
n++ common = array.every(lowestCommon) } return n}

Lets us go through this’n step by step shall we?

First we get some of those tricky variables set up, as we do not know in what order the array of two numbers we are given is in, lets make sure to get the max and the min values in that array instead of taking the 0th and 1st values.

Math.max() and Math.min() are perfect for this, and by using the spread operator on the array (…arr) as the methods do not take arrays as arguments we can quickly and easily sort that out.

let min = Math.min(...arr)let max = Math.max(...arr)let array = []

We also want to create a nice empty array to fill with all the values between min and max, the reason behind this is to let us get to the rather handy .every() array method that JS has. We then fill that array now using a simple for loop.

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

Now we head on down to making a nice little function that checks whether or not a variable being passed in (n) can be divisible by a second variable (currentValue) and leave no remainder. We declare this function as it is an important aspect in the .every() javascript method.

const lowestCommon = (currentValue) => n % currentValue === 0;

Now knowing that the smallest possible number divisible by all the other numbers could be the maximum number in the array * (itself-1) we set that as the lowest value to be checked (counting all the way up from 1 seems like a waste of good computing power, especially when the min number in the array could be something like 700).

let n = max* (max-1)

We also want to set a variable as false here, as soon as it becomes true (thanks to whats coming up next) it breaks a loop and returns us a number that is going to be our answer.

let common = false

The definition of the every method from Mozilla webdocs is as follows

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

This is exactly what we want, hence why we made the lowestCommon function a minute ago. Passing it into the every method whilst also incrementing n means that it will continue to run in this loop until common === true.

Before we get to our while loop we just double check that our initial value to n isn’t the number we are looking for

common =  array.every(lowestCommon)

Now we are out of our while loop. This will check every time a value is passed in whether or not the .every() method has changed common to true. We make sure to increment n before the test, as if we do it afterwards our n value with be 1 more than our desired result, and that would be silly!

while (common === false){     n++     common =  array.every(lowestCommon)}

When common === true the loop stops and n will be our Lowest common denominator and we go ahead and return it! Hurray!

return n

Now everyone pause, have a biscuit and a leg stretch and we will go into the gnarly depths of doing this in ruby.

You all ready to get back into it?

Let’s go.

def lowestCommon(arr)     max = arr.max()     min = arr.min()     array = (min..max).to_a     array.reduce(:lcm)end

Well isn't that lovely. Ruby does a lot of the work for us.

Sure we still need to make an array of the values betwixt max and min (but we can now just use rubys spread operator). However this time we can now just use the reduce method and pass in the wonderful least common multiple method that ruby has as the block!

I have spread the method out for ease of reading, however if we liked we could get it down to one line!

def lowestCommon(arr)(arr.min()..arr.max()).to_a.reduce(:lcm)end

I hope this has been helpful all!

Next time I think I shall cover the sum of all Primes equal to or under a given number. If you want to give it a go in the meantime try it out yourselves!

If you have any questions make some comments and I shall do my darnedest to help out!

Keep calm and code on.

Dan

--

--

The Startup
The Startup

Published in The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

Daniel Andrew
Daniel Andrew

Written by Daniel Andrew

Old enough to have enjoyed Sid Miers Pirates on Windows 3.1 and deciding to delve out into more interesting waters.

No responses yet