-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path012.js
90 lines (67 loc) · 1.82 KB
/
012.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
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* Highly Divisible Triangular Number
* Computes the first triangle number to have over five hundred divisors
*
* Ashen Gunaratne
* mail@ashenm.ml
*
*/
/**
* @function triangle
* @summary Computes the first triangle number with over n divisors
* @param {Number} n
* @description Returns the first triangle number with over n divisors
*/
const triangle = function computeTriangleWithDivisors(n) {
let triangular;
for (let factors = 1, index = 2; factors <= n; index++) {
factors = divisors(triangular = index * (index + 1) / 2);
}
return triangular;
};
/**
* @function devisors
* @summary Computes the number of divisors
* @param {Number} number
* @description Returns the number of divisors of the parameterised integer
*/
const divisors = function computeNumberOfDivisors(number) {
let counter = 1;
let divisor = 2;
let exponent = 0;
while (number !== 1) {
if (number % divisor !== 0) {
counter *= exponent + 1;
divisor = next(divisor);
exponent = 0;
continue;
}
number /= divisor;
exponent += 1;
}
return counter *= exponent + 1;
};
/**
* @function next
* @summary Computes the next prime
* @param {Number} current
* @description Returns the prime number followed by the parameterised integer
*/
const next = function computeNextPrime(current) {
let next = current + 1;
for (let divisor = 2, ceiling = Math.sqrt(next); divisor <= ceiling;) {
if (next % divisor !== 0) {
divisor++;
continue;
}
ceiling = Math.sqrt(next += 1);
divisor = 2;
}
return next;
};
// tests
console.assert(triangle(1) === 3, `expected 3 got ${triangle(1)}`);
console.assert(triangle(3) === 6, `expected 6 got ${triangle(3)}`);
console.assert(triangle(5) === 28, `expected 28 got ${triangle(5)}`);
// answer
console.log(triangle(500));