-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5-smallest_multiple.js
41 lines (33 loc) · 989 Bytes
/
5-smallest_multiple.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
/*
Project Euler: Problem 5: Smallest multiple
2520 is the smallest number that can be divided by each of the
numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by
all of the numbers from 1 to n?
*/
function smallestMult(n) {
let current = n;
let isDivisable = false;
while (!isDivisable) {
for (let i = n - 1; i > 1; i -= 1) {
if (current % i === 0) {
isDivisable = true;
} else {
isDivisable = false;
current += n;
break;
}
}
}
return current;
}
const br = `\n---------------------------------------------\n`;
var timeit = function (label, f, ...args) {
console.time(label);
result = f(...args);
console.log(`${br}RESULT RETURNED: ${result}`);
console.timeEnd(label);
};
timeit(`RESULT EXPECTED: 60\t\tTime`, smallestMult, 5);
timeit(`RESULT EXPECTED: 420\t\tTime`, smallestMult, 7);
timeit(`RESULT EXPECTED: 232792560\t\tTime`, smallestMult, 20);