Skip to content

Commit 8a1e74f

Browse files
authored
Merge pull request TheAlgorithms#491 from Waddah-JD/project-Euler-problem-009-solution
Project Euler problem 009 solution
2 parents 610064d + 115ca0f commit 8a1e74f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Project-Euler/Problem9.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Special Pythagorean triplet
3+
4+
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
5+
6+
a^2 + b^2 = c^2
7+
For example, 32 + 42 = 9 + 16 = 25 = 52.
8+
9+
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
10+
Find the product abc.
11+
*/
12+
13+
const isPythagoreanTriplet = (a, b, c) => Math.pow(a, 2) + Math.pow(b, 2) === Math.pow(c, 2)
14+
15+
const findSpecialPythagoreanTriplet = () => {
16+
for (let a = 0; a < 1000; a++) {
17+
for (let b = a + 1; b < 1000; b++) {
18+
for (let c = b + 1; c < 1000; c++) {
19+
if (isPythagoreanTriplet(a, b, c) && a + b + c === 1000) {
20+
return a * b * c
21+
}
22+
}
23+
}
24+
}
25+
}
26+
27+
console.log(findSpecialPythagoreanTriplet())

0 commit comments

Comments
 (0)