From 24a8bbee705134d0fa031d73fcd1669ab3c0c96b Mon Sep 17 00:00:00 2001 From: Aditya Date: Fri, 26 Oct 2018 21:08:17 +0530 Subject: [PATCH] [Guide] Project Euler: Problem 9: Special Pythagorean triplet (#28382) * feat: added problem 9 * Removed extra word and added formatting to variable names --- .../index.md | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/guide/english/certifications/coding-interview-prep/project-euler/problem-9-special-pythagorean-triplet/index.md b/guide/english/certifications/coding-interview-prep/project-euler/problem-9-special-pythagorean-triplet/index.md index 6b621edda9..b931af5cb4 100644 --- a/guide/english/certifications/coding-interview-prep/project-euler/problem-9-special-pythagorean-triplet/index.md +++ b/guide/english/certifications/coding-interview-prep/project-euler/problem-9-special-pythagorean-triplet/index.md @@ -3,8 +3,31 @@ title: Special Pythagorean triplet --- ## Problem 9: Special Pythagorean triplet -This is a stub. Help our community expand it. +### Method: +- 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. -This quick style guide will help ensure your pull request gets accepted. +### Solution: +```js +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); +``` +- [Run Code](https://repl.it/@ezioda004/Project-Euler-Problem-9-Special-Pythagorean-triplet) + +### References: +- [Wikipedia](https://en.wikipedia.org/wiki/Pythagorean_triple)