JavaScript Implementation of Problem 9
View source code here on GitHub!
- p0009()
Project Euler Problem 9
Brute force on this problem has been very effective, so why stop now
Problem:
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.
- Returns:
number --
1/**
2 * Project Euler Problem 9
3 *
4 * Brute force on this problem has been very effective, so why stop now
5 *
6 * Problem:
7 *
8 * A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
9 * a**2 + b**2 = c**2
10 *
11 * For example, 3**2 + 4**2 = 9 + 16 = 25 = 5**2.
12 *
13 * There exists exactly one Pythagorean triplet for which a + b + c = 1000.
14 * Find the product abc.
15 *
16 * @return {number}
17 */
18exports.p0009 = function() {
19 for (let c = 3; ; c++) {
20 const cSquared = c * c;
21 for (let b = 2; b < c; b++) {
22 const bSquared = b * b;
23 for (let a = 1; a < b; a++) {
24 const aSquared = a * a;
25 if (aSquared + bSquared == cSquared && a + b + c === 1000) {
26 return a * b * c;
27 }
28 }
29 }
30 }
31 return 0;
32};