Spoiler Spoiler SpoilerI just picked a random question,
Problem 9...
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a² + b² = c²
For example, 3² + 4² = 9 + 16 = 25 = 5².
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
There isn't enough information here to
solve the problem, meaning we're going to be left with some amount of brute force to find the triplet in question. Sooooooo... the lazy solution!
The problem defines a right triangle with a perimeter of 1000 units. Since we know the second-shortest leg of a right triangle can be up to oh-so-slightly less than the length of the hypotenuse, we know that the length of that side in this case cannot meet or exceed 1000 / 2 = 500 units, meaning an upper theoretical limit of 499.
This gives us a nested loop. For each value of
a from 1 to 498, then each value of
b from
a + 1 to 499, which pair of values solves the problem as stated?
In JavaScript:
function triplet() {
var a, b, c;
// Brute force the legs of the triangle
for (a = 1; a < 498; a++) {
for (b = a + 1; b < 499; b++) {
// Calculate the length of the hypotenuse
c = Math.sqrt(a * a + b * b);
// Reject the hypotenuse if it is non-integer
if (c % 1) continue;
// Check if the perimeter is 1000
if (a + b + c == 1000)
return "a = " + a + ", b = " + b + ", c = " + c;
} // b
} // a
// A perimeter of 1000 was not found
return "I dun' find it!";
}
The output of this function is: a = 200, b = 375, c = 425. The product, therefore, is 200 * 375 * 425 = 31875000.