Books I Like

Saturday, November 14, 2009

Solving Project Euler Problem #9 No code Required

Problem Statement :-

A Pythagorean triplet is a set of three natural numbers, a b c, for which,

a^2 + b^2 = c^2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

Solution :-
Lets have a look on the first Pythagorean triplet 3, 4 and 5.

All their multiples, would also be a Pythagorean triplet (e.g. 6, 8 and 10 or 9, 12 and 15 , and so on..)

Also, any odd number can be represented as x^2 - y^2
For e.g.
3 = 2^2 - 1^2
5 = 3^2 - 2^2
7 = 4^2 - 3^2

Since, (x^2 - y^2)^2 + (2xy)^2 = (x^2 + y^2)^2, we can always generate a triplet for any odd number x^2 - y^2.

So, using this any triplet is of the form :
k*(x^2-y^2), k*2xy and k*(x^2+y^2), where k is any number (1, 2, ...n).

Well, now let us solve the current problem.

a = k*(x^2-y^2)
b = k*2xy
c = k*(x^2+y^2)
Since a+b+c = 1000, and they are the sides of a triangle,

a,b,c < 500
=> k*(x^2+y^2) < 500
=> x,y < 500^(1/2) < 23

Now, given a+b+c = 1000
=> k(x^2-y^2+2xy+x^2+y^2) = 1000
=> 2kx(x+y) = 1000
=> k*x(x+y) = 500 (Factors of 500 is , 2,2,5,5,5)
=> k*x(x+y) = 20*(20+5) (This is the only value, which satisfies x,y < 23)
=> for k =1 , x = 20, y = 5

So, a = 20^2 - 5^2 = 375
b = 2*20*5 = 200
c = 20^2 + 5 ^2 = 425

So, the product is : 375*200*425 = 31875000

No comments:

Get at Amazon