Problem description:
http://projecteuler.net/index.php?section=problems&id=9
A Pythagorean triplet is a set of three natural numbers, a
b
c, for which,
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
My solution:
The logic behind:
My initial approach was to simplify the equation by using the given information and some algebra, resulting in the following equation:
b^2 = c2 – a2
b = 1000 – a – c
(1000 – a – c)2 = c2 – a2
10002 – 1000a – 1000c – 1000a – a2 – ac – 1000c – ac – c2 = c2 – a2
2000a + 2000c + 2ac = 10002
1000a + 1000c + ac = 500000
Eventually i reconsidered my approach.
As i later was to discover, this pitch wasn’t a bad idea after all, but i eventually went for a good ol’ brute force.
The equation could have been used to shorten the time required to solve the problem by brute force a lot, but my solution spits out the correct answer within 5 secs so what the heck.
Solution (PHP):
<?php for ($a = 1; $a < 800; $a++) { for ($b = $a; $b < 1000; $b++) { $c = 1000-$a-$b; if (bcpow($a,2)+bcpow($b,2))==bcpow($c,2) //GREAT SUCCESS! { echo $a*$b*$c; } } } ?>
