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:

Anonymous said...

If you don’t see an email in your “primary” inbox, certain to|make positive to|remember to} check your “spam” folder before requesting another email. We have details on the highest Michigan on-line casinos assist you|that will assist you|that can help you} find one of the best welcome provides, bonuses and game choices. You must deposit minimal of|no much less than} $10 온라인 카지노 to qualify, however Neteller and Skrill customers usually are not entitled to it. In the early 2000s, on-line casinos began providing reside vendor games, allowing players to enjoy remote playing however nonetheless play with a human vendor.