Tuesday, June 3, 2008

Project Euler Problem 9 in C#

So I have just finished Problem 9. You have got to love the Pythagorean Theorem. I have always been a math geek and I was awestruck when my preCalculus teacher took the time to prove it. Let me tell you. That was the longest bit of logic that I had seen in my 17 years of life. But there it was all 2 whiteboards full. It would have been 3 but he ran out of room.

Well, problem number 9 was an interesting return to the world of Pythagoras. If you haven't looked at the riddle, here it is:

Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000.

OK. The first thing we know is that a2 + b2 = c2 is Pythagorean Theorem.
Now to find Pythagorean Triplets with positive whole numbers(natural numbers) we could try iterating through the infinite set of number until we found the right set or we could use this formula:

a = 2mn
b = m2 - n2
c = m2 + n2
where m > n > 0
So now we have:

1000 = a + b + c
1000 = (2mn) + (m2 - n2) + (m2 + n2)
1000 = 2mn + 2m2 + 0
500 = mn + m2
500 = m(n + m)
(500/m) = n + m
(500/m) - m = n

You now have what you need to write a program to find your triplet. I'll give you my inplementation later.

1 comment:

for IT the said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru Dot Net Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.