-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path009.js
35 lines (30 loc) · 870 Bytes
/
009.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
/**
* A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
* a2 + b2 = c2
* For example, 32 + 42 = 9 + 16 = 25 = 52.
* There exists exactly one Pythagorean triplet for which a + b + c = 1000.
* Find the product abc.
*/
function findPythagoreanTriplet(limit) {
let a = 0;
let b = 0;
let c = 0;
let gotcha = 0;
for (let i = 1; i < limit; i++) {
for (let j = 1; j < limit; j++) {
let powedNumber = Math.pow(i, 2) + Math.pow(j, 2);
if (powedNumber > 0 && Math.sqrt(powedNumber) % 1 === 0) {
a = i;
b = j;
c = Math.sqrt(powedNumber);
if (a + b + c === limit) {
gotcha = a * b * c;
}
}
}
}
let formattedString =
'a: ' + a + ' b: ' + b + ' = c: ' + c + ' gotcha: ' + gotcha;
return formattedString;
}
console.log(findPythagoreanTriplet(1000));