2018-10-12 15:37:13 -04:00
---
2019-07-30 00:25:58 +05:30
title: 'Problem 9: Special Pythagorean triplet'
2018-10-12 15:37:13 -04:00
---
2019-07-24 00:59:27 -07:00
# Problem 9: Special Pythagorean triplet
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Problem Explanation
2018-10-26 21:08:17 +05:30
- In this challenge we need to find the pythagorean triple.
- We have the following information - `a < b < c`
- Based on this, we can make a loop starting from `a = 0` and `b = a` since `a < b` always.
- We also know that `a + b + c = n` and `a^2 + b^2 = c^2` , since we have `a` , `b` and `n` . We can find `c` and see if it satisfies the triplet theorem.
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Solutions
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
2018-10-26 21:08:17 +05:30
```js
function specialPythagoreanTriplet(n) {
let sumOfabc = n;
2019-07-24 00:59:27 -07:00
for (let a = 1; a < n ; a + + ) {
for (let b = a; b < n ; b + + ) {
let c = n - a - b;
if (c > 0) {
if (c ** 2 == a ** 2 + b ** 2) {
return a * b * c;
2018-10-26 21:08:17 +05:30
}
}
}
2019-07-24 00:59:27 -07:00
}
2018-10-26 21:08:17 +05:30
}
2018-10-12 15:37:13 -04:00
2018-10-26 21:08:17 +05:30
specialPythagoreanTriplet(1000);
```
2019-07-24 00:59:27 -07:00
#### Relevant Links
2018-10-26 21:08:17 +05:30
- [Wikipedia ](https://en.wikipedia.org/wiki/Pythagorean_triple )
2019-07-24 00:59:27 -07:00
< / details >