-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem12.js
51 lines (40 loc) · 996 Bytes
/
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
/*
Problem 12
Triangle Numbers
Need 2 functions
1. Find the factors of a number
2. Find the nth triangle number
*/
// find the number of factors of a number (return as an integer)
function factors(x) {
let count = 0;
let sqrt = Math.sqrt(x);
for (let i = 1; i <= sqrt; i++) {
if (x % i === 0) {
count += 2; // Count both the divisor and its corresponding quotient
}
}
// If x is a perfect square, subtract one from the count
if (sqrt * sqrt === x) {
count--;
}
return count;
}
// find the nth triangle number (and return it)
function triangle(n) {
return (n * (n + 1)) / 2;
}
// get a series of triangle numbers, and list their number of factors
for (let i = 1; i <= 10; i++) {
//console.log(factors(triangle(i)))
}
let facmax = 0;
let n = 10000;
while (facmax < 500) {
n++
facmax = factors(triangle(n));
console.log();
console.log(n);
console.log(facmax);
console.log(triangle(n));
}