Smallest Common Multiple - JavaScript Solution and Walkthrough
(14/21) Learn how to solve coding challenges using FreeCodeCamp's curriculum.
14/21 Smallest Common Multiple
Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.
The range will be an array of two numbers that will not necessarily be in numerical order.
For example, if given 1 and 3, find the smallest common multiple of both 1 and 3 that is also evenly divisible by all numbers between 1 and 3. The answer here would be 6.
function smallestCommons(arr) {
return arr;
}
smallestCommons([1,5]);
Credit: FreeCodeCamp.org
Understanding the Challenge
First, let's get a better understanding of the challenge before we attempt a solution.
In this challenge, the function you're to complete takes an array as a parameter. This array contains two numbers.
The smallestCommons()
function is to return the smallest common mutiple of both numbers and is also divisible by all numbers between the two given numbers.
In other words, the number to be returned should be the smallest possible number that is divisible by all numbers from the least of the two given numbers to the greater.
Let's take an example to make things clearer.
Assume the given array contains 1 and 4. Then, smallesCommons([1, 4])
should return 12
This is because 12
is the least possible number divisible by all numbers from 1 to 4 (that is 1, 2, 3, and 4).
Note that in the challenge description you're informed the two numbers in the array "will not necessarily be in numerical order."
Pseudocode
Given an array of two number
Sort the array in ascending order
Get all numbers between the two given numbers (inclusive)
Multiply all numbers to get at least one common multiple
Loop through all multiples of the biggest given number
For every iteration of the loop
Check if all numbers in the given range can divide the multiple
If yes, return the multiple
Solving the Challenge
First, we need to sort the given array. That way, we can get to know which of the two given numbers is the less (let's call it smallNum
) and which is greater (let's call it bigNum
).
const sortedArray = arr.sort((a, b) => a - b)
const smallNum = sortedArray[0]
const bigNum = sortedArray[1]
We also need to know how many numbers are in present in the range of numbers from smallNum
to bigNum
. Let's save it in a variable called range
.
const range = bigNum - smallNum + 1;
Next, we need to find at least one number that can be divided by all the numbers in the range. We can easily find a common multiplier by multiplying all the numbers in the range.
let commonMultiple = 1;
for (let i = smallNum; i <= bigNum; i++) {
commonMultiple *= i;
}
We cannot return this as the final answer because it is possible for there to be another multiple that is lesser. So we need to find out if there is.
To do so, we loop through all the multiples of bigNum
from bigNum
to commonMultiple
. For every iteration of the loop, we check if i
(which will be a multiple of bigNum
) is also a multiple of all the other numbers in the range.
for (let i = bigNum; i <= commonMultiple; i += bigNum) {
let count = 0;
for (let j = smallNum; j <= bigNum; j++) {
if (i % j === 0) {
count += 1;
}
}
if (divisors === range) {
return i;
}
}
We do that by running a count of how many of the numbers in range
can perfectly divide i
. If we can get an i
whose count equals range
, we return that i
.
Final Solution
function smallestCommons(arr) {
const sortedArray = arr.sort((a, b) => a - b);
const smallNum = sortedArray[0]
const bigNum = sortedArray[1]
const range = bigNum - smallNum + 1;
let commonMultiple = 1;
for (let i = smallNum; i <= bigNum; i++) {
commonMultiple *= i;
}
for (let i = bigNum; i <= commonMultiple; i += bigNum) {
let count = 0;
for (let j = smallNum; j <= bigNum; j++) {
if (i % j === 0) {
count += 1;
}
}
if (count === range) {
return i;
}
}
}
smallestCommons([1, 5]); // 60
Congratulations!
You just cracked the 14th challenge in this series.
Cheers and happy coding!