-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path005.js
More file actions
63 lines (56 loc) · 1.15 KB
/
005.js
File metadata and controls
63 lines (56 loc) · 1.15 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 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 20?
function primeFactors(n) {
var factors = [];
var d = 2;
while (n > 1) {
while (n % d === 0) {
factors.push(d);
n = n/d;
}
d++;
if (d*d > n && n > 1) {
factors.push(n);
break;
}
}
return factors;
}
function minPolynomial(p1, p2) {
var result = [];
var x, y;
while (p1.length || p2.length) {
if (p1.length === 0) {
result.push( p2.shift() );
} else if (p2.length === 0) {
result.push( p1.shift() );
} else {
x = p1[0];
y = p2[0];
if (x < y) {
result.push( p1.shift() );
} else if (x > y) {
result.push( p2.shift() );
} else {
result.push( p1.shift() );
p2.shift();
}
}
}
return result;
}
function minCommonMultiple(min, max) {
var factors = [];
while (min <= max) {
factors = minPolynomial(factors, primeFactors(min));
min++;
}
var multiple = 1;
while (factors.length) {
multiple *= factors.shift();
}
return multiple;
}
console.log( minCommonMultiple(1, 20) );