Project Euler - Pythagorean Triplets
I’ve been trying to crank through Project Euler questions over the past week or so. They are pretty challenging at first sight, but I’m finding that after looking at them for a few minutes, patterns start revealing themselves and the problem becomes much clearer. Today’s question is the following:
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
I figure that the best way to approach these problems is to break them down in to manageable chunks that I can chain together to get a final answer. In this situation, I broke the problem down like this:
1) Initialize an instance of a class with an instance variable equal to the number that is the sum of a + b + c.
2) Get all three number combinations that add up to this number.
3) Filter these combinations to those where a < b < c
4) Check available combinations for pythagorean triplet-ness
5) Find the product of the numbers in the triplet.
The Breakdown
Initialize an instance of a class with an instance variable equal to the number that is the sum of a + b + c.
1 2 3 |
|
Pretty straightforward. Set the @sum instance variable to whatever the sum is that we’re given at the start of the problem (in this case, 1000).
Get all three number combinations that add up to the sum.
1 2 3 4 5 6 7 8 9 10 11 |
|
I’m creating a series of three nested loops, where the variable being set up for each loop is a, b, and c, respectively. Within the last loop I add [a,b,c] to the cobinations_adding_to_n array if the sum of a,b, and c is equal to the sum we’ve initialize the class instance with.
Filter the combinations to those where a < b < c
1 2 3 4 5 |
|
We use the .collect method to create an array where the only items that are stored are those from combinations where a < b < c, or the the array’s value at 0 index is less that its value at 1 index and its value at 1 index is less than its value 2 index. I use compact at the end of this to get rid of nil values in the array.
Check available combinations for pythagorean triplet-ness
I built this method to check if an array [a,b,c] is a pythagorean triplet:
1 2 3 |
|
Test each of your possible combinations for pythagorean triplet-ness:
1 2 3 4 5 |
|
This will provide the one array where [a,b,c] is a pythagorean triplet. On to our last step:
Find the Product of the Numbers in the Triplet
1 2 3 |
|
That’s it! We get the when a+b+c = 1000, we run:
1 2 |
|
And our answer is… Ha. You thought I’d give it away here. You’ll have to run this code yourself to find out!