Project Euler: #9 - Special Pythagorean triplet

Search for a command to run...

No comments yet. Be the first to comment.
In this series, I will be discussing Project Euler Solutions. 'Project Euler' helps in improving problem-solving and programming skills. Remember, there is always a better solution to the problem. 💯
Problem The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. Problem Description The problem is pretty self-explanatory! Sum of primes below 10 is 17. Find the sum of primes below 2000000. Approach...
What is JavaScript? Discover how this quirky yet powerful language runs in browsers, what makes it tick, and how you can start adding it to your sites

Problem If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters w...

Problem 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000? Problem Description The problem is pretty self-explanatory. 215 is 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26....

Problem Starting in the top left corner of a 2*2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner. How many such routes are there through a 20*20 grid? Problem Description We are given a...

Problem The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → ...

A Pythagorean triplet is a set of three natural numbers, a < b < c for which,
$$a^2 + b^2 = c^2$$
For example,
$$3^2 + 4^2 = 9 + 16 = 25 = 5^2$$
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
In a Pythagorean triplet of natural numbers, a < b < c for which,
$$a^2 + b^2 = c^2$$
Example,
$$3^2 + 4^2 = 9 + 16 = 25 = 5^2$$
In this case a + b + c is 12 and a * b * c is 60
Same way, there exists one triplet, where a + b + c = 1000 and we need to find a * b * c of that triplet.
Let's approach this problem as finding some triplets where a + b + c = n.
There are as many different mathematical approaches to solving this(I have known only 2. But Wikipedia has more than that 😎).
The straightforward approach is to loop over a and b to check,
$$a^2 + b^2 = (n − a − b)^2$$
And since we have a < b < c, we use the below formula while looping through a and b - a <= (n - 3)/3 & b < (n - a)/2
const pythagoreanTriplet = n => {
let maxProduct = -1;
for(let a = 1; a <= (n - 3)/3; a++) {
for (let b = a; b <= (n - a)/2 ; b++) {
if( (a*a) + (b*b) === (n-a-b)*(n-a-b)) {
maxProduct = a*b*(n-a-b);
}
}
}
return maxProduct;
}
console.log(pythagoreanTriplet(1000));
You can find my solution on GitHub 09. My GitHub solution is different from the above. To achieve 100% success(all test cases passed) in Hacker Rank, I tweaked a bit using the formula.
If you have another or a better solution, please leave it in the comments below.
For the other Project Euler Solutions, please follow the series Project Euler Solutions in JS.
Thank you!