Last active
January 6, 2016 03:13
-
-
Save jayrbolton/fb30f0cffc3c38860f24 to your computer and use it in GitHub Desktop.
pe #9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// project euler #9 | |
// jay r bolton 1/5/2016 | |
// | |
// | |
// A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, | |
// | |
// a2 + b2 = c2 | |
// For example, 32 + 42 = 9 + 16 = 25 = 52. | |
// | |
// There exists exactly one Pythagorean triplet for which a + b + c = 1000. | |
// Find the product abc. | |
// iterate over numbers [a, a+1, ..], [b, b+1, ...], [c, c+1, ...] and find the triplet that satisfies a^2 + b^2 = c^2 and a + b + c = 1000 | |
// constraints/filters: | |
// - a < b < c < (1000/3) | |
// | |
// brute forcing 333 * 333 * 333 iterations will be 36926037 iterations -- we can probably cut that down... | |
// | |
// Really easy in haskell: | |
// [a * b * c | a <- [200..500], b <- [a+1..500], c <- [b+1..500], a^2 + b^2 + c^2 == 1000 && a + b + c == 100] | |
let answer = false | |
for(let a = 200; a < 500 && !answer; ++a) | |
for(let b = a+1; b < 500 && !answer; ++b) | |
for(let c = b+1; c < 500 && !answer; ++c) | |
if(Math.pow(a, 2) + Math.pow(b, 2) == Math.pow(c, 2) && a + b + c == 1000) | |
answer = a * b * c | |
console.log('answer is', answer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment