* fix: restructure certifications guide articles * fix: added 3 dashes line before prob expl * fix: added 3 dashes line before hints * fix: added 3 dashes line before solutions
955 B
955 B
title
| title |
|---|
| Special Pythagorean triplet |
Problem 9: Special Pythagorean triplet
Problem Explanation
- 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 = 0andb = asincea < balways. - We also know that
a + b + c = nanda^2 + b^2 = c^2, since we havea,bandn. We can findcand see if it satisfies the triplet theorem.
Solutions
Solution 1 (Click to Show/Hide)
function specialPythagoreanTriplet(n) {
let sumOfabc = n;
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;
}
}
}
}
}
specialPythagoreanTriplet(1000);