-
Notifications
You must be signed in to change notification settings - Fork 0
/
Euler-9.js
35 lines (33 loc) · 861 Bytes
/
Euler-9.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
function specialPythagoreanTriplet (num) {
const sumOfabc = num
function gcd (a, b) {
if (!b) { return a }
return gcd(b, a % b)
}
const softLimit = Math.floor(Math.sqrt(sumOfabc / 2))
// Euclid says Hi! again
let k = 0
let d = 0
let n = 0
for (let m = 2; m <= softLimit; m++) {
if ((n / 2) % m === 0) {
if (m % 2 === 0) { // ensure that we find an odd number for k
k = m + 1
} else {
k = m + 2
}
while (k < 2 * m && k <= sumOfabc / (2 * m)) {
if (((sumOfabc / (2 * m)) % k === 0) && (gcd(k, m) === 1)) {
d = sumOfabc / 2 / (k * m)
n = k - m
const a = d * (m ** 2 - n ** 2)
const b = 2 * d * n * m
const c = d * (m * m + n * n)
return a * b * c
}
k += 2
}
}
}
}
specialPythagoreanTriplet(1000)