-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem12.js
77 lines (68 loc) · 1.59 KB
/
problem12.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module.exports = function() {
function triangle(n) {
return n*(n+1)/2;
}
function numOfDivisorsBruteForce(n) {
var count = 0;
for(var i = 1; i <= n; i++) {
if(n%i === 0) count++;
}
return count;
}
function isPrimeNaive(n) {
if(n < 2) return false;
else if(n === 2 || n === 3) return true;
else if(n%2 === 0 || n%3 === 0) return false;
else {
var maxDivisor = Math.sqrt(n);
for(var i = 5; i <= maxDivisor; i+=6) {
if(n % i === 0 || n%(i + 2) === 0) return false;
}
return true;
}
}
function numOfDivisorsEloquent(n) {
var primeDivisors = {};
for(i = 2; i < n; i++) {
var count = 0;
while(n%i === 0) {
count++;
n = n/i;
}
if(count > 0) {
primeDivisors[i] = count;
count = 0;
}
if(solution.isPrime(n) && n !== 1) {
count++;
primeDivisors[n] = count;
n = 1;
count = 0;
}
}
var result = 1;
for(var key in primeDivisors) {
if(primeDivisors.hasOwnProperty(key)) {
result *= primeDivisors[key] + 1;
}
}
return result;
}
function run(n) {
var i = 1;
var numberDivisors = 0;
while(numberDivisors <= n) {
number = solution.triangle(i);
numberDivisors = solution.numOfDivisors(number);
console.log('Number: ' + number + ' ---->Number of Divisors: ' + numberDivisors);
i++;
}
}
var solution = {
triangle: triangle,
numOfDivisors: numOfDivisorsEloquent,
run: run,
isPrime: isPrimeNaive
};
return solution;
};