-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmallestCommonMultiple.js
More file actions
43 lines (34 loc) · 1.45 KB
/
smallestCommonMultiple.js
File metadata and controls
43 lines (34 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
Source: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple
--Problem--
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.
--Test Cases--
- smallestCommons([1, 5]) and smallestCommons([5,1]) should return 60.
- smallestCommons([2, 10]) should return 2520
- smallestCommons([1, 13]) should return 360360
- smallestCommons([23, 18]) should return 6056820
*/
function smallestCommons(arr) {
/*
1. Find min and max in the provided arr.
2. First let common = max*(max-1). This is the smallest number that can possibly be divided by all numbers ranging from min to max.
3. If not divisible, check next multiple of max.
4. Repeat until we find the lowest common multiple.
*/
const [min, max] = arr.sort((a,b) => a-b);
let divisible, x = 0, common;
do {
divisible = true;
common = max*(max-1) + x*max;
for (let i = max; i >= min; i--) {
if (common%i != 0) {
divisible = false;
}
}
x++;
} while (!divisible);
return common;
}
console.log(smallestCommons([1,5]));