Files
mrugesh 91df817cfe fix(guide) add stubs, update spellings and prepare for move (#36531)
* fix(guide) add stubs and correct file path misspellings and pr… (#36528)

* fix: corrected file path to match curriculum

* fix: renamed to newer challenge name

* fix: added solutions to articles from challenge files

* fix: added missing .english to file name

* fix: added missing title to guide article

* fix: correct solution for guide article

* fix: replaced stub with hint

* fix: added space in Hint headers

* fix: added solution to guide article

* fix: added solution to guide article

* test: replaced stub with hint and solution

* fix: add Problem number: to title

* fix: changed generatorexponential to correct name

* fix: renamed knight's tour to knights-tour

* fix: updated guide article
2019-07-30 00:25:58 +05:30

968 B

title
title
Problem 9: 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 = 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.

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);