Tag Archive for 'pythagoreanTriplet'

Project Euler : Finding Pythagorean Triplet

Well well, this was so far the easiest problem of the lot. The requirement is to find a Pythagorean triplet a, b, c such that

a + b + c = 1000

I was able to solve this question using pencil and paper.

A primitive Pythagorean triplet is one for which:

a^2 + b^2 = c^2;

gcd(a,b) = gcd(b,c) = gcd(a,c) = 1

Such a triplet can be written in the following parametrized form:

a = m^2 – n^2

b = 2mn

c = m^2 + n^2

for integers m, n. Using the parametrized form for a, b, c and the condition of their sum being 1000, we get the following:

a + b + c = 1000

=> 2m^2 + 2mn = 1000

=> m^2 + mn – 500 = 0

Roots of this equation can be easily found as m = 20 and n = 5. Thus we have that

a = 375, b = 200 and c = 425

And our problem is solved.

Now what if we replace 1000 by s. In that case the quadratic in m changes and we will have to code a small loop to determine an integral value for m, n. But even that is straight forward and I see not difficulty in it.

Popularity: 2% [?]