-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp005.js
More file actions
40 lines (37 loc) · 816 Bytes
/
p005.js
File metadata and controls
40 lines (37 loc) · 816 Bytes
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
Array.prototype.count = function(n) {
var count = 0;
for (var i = 0; i < this.length; i++) {
if (this[i] == n) count++;
}
return count;
}
function primeFactors(n) {
var f = [];
var d = 2;
while (n > 1) {
while (n%d==0) {
f.push(d);
n = n/d;
}
d++;
}
return f;
}
function smallestDivTo(x) {
var factors = [];
for (var i = 2; i <= x; i++) {
var f = primeFactors(i);
for (var j = 0; j < f.length; j++) {
if (!factors[f[j]] || (f.count(f[j]) > factors[f[j]])) factors[f[j]] = f.count(f[j]);
}
}
var result = 1;
for (var i = 0; i < factors.length; i++) {
if (factors[i]) result *= Math.pow(i, factors[i]);
}
return result;
}
test("smallestDivTo", function() {
equal(smallestDivTo(10), 2520);
equal(smallestDivTo(20), 232792560);
});