-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path005.js
53 lines (42 loc) · 1.07 KB
/
005.js
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
44
45
46
47
48
49
50
51
52
53
/**
* Smallest Multiple
* https://projecteuler.net/problem=5
* Computes the LCM for a integer range
*
* Ashen Gunaratne
*
*/
/**
* @function rlcm
* @summary Computes the LCM for a integer range
* @param {Number} start - The starting value
* @param {Number} end - The ending value (inclusive)
* @description
*/
const rlcm = function computeLeastCommonMultiple(start, end) {
let lcm = start;
while (start !== end) {
lcm = lcm * (start += 1) / gcd(lcm, start);
}
return lcm;
};
/**
* @function gcd
* @summary Computes the GCD of two integers
* @param {Number} x
* @param {Number} y
* @description Computes GCD of two intergers using Euclid's algorithm
*/
const gcd = function computeGreatestCommonDivisor(x = 1, y = 1) {
while (y !== 0) {
[ x, y ] = [ y, x % y ];
}
return x;
};
// tests
console.assert(gcd(2, 3) === 1, `expected 1 got ${gcd(2, 3)}`);
console.assert(gcd(48, 18) === 6, `expected 6 got ${gcd(48, 18)}`);
console.assert(rlcm(1, 10) === 2520, `expected 2520 got ${rlcm(1, 10)}`);
// answer
console.log(rlcm(1, 20));