-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path009.js
38 lines (27 loc) · 800 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
36
37
38
/**
* Special Pythagorean Triplet
* Computes the product of Pythagorean triplet yielding sum 1000
*
* Ashen Gunaratne
*
*/
/**
* @function triplet
* @summary Computes the product of a Pythagorean triplet yielding sum n
* @param {Number} n - the sum of the Pythagorean triplet
* @description Returns the product of a Pythagorean triplet yielding the parameterised sum n
*/
const triplet = function computeProductOfPythagoreanSum(n) {
const ceil = n / 2;
for (let a = 1, b = 1; a <= ceil;) {
let c = Math.sqrt(a * a + b * b);
if (a + b + c === n) { return a * b * c; }
if ((b += 1) > ceil) { a += 1; b = 1; }
}
return -1;
};
// tests
console.assert(triplet(12) === 60, `expected 60 got ${triplet(12)}`);
// answer
console.log(triplet(1000));