# Pythagorean triplets

Published on 2011-07-22It's been my impression so far that taking on Project Euler problems has been made me think more logically and perhaps more thoroughly. I would hesitate before saying that it has made me think more *mathematically*, as my approach in solving the problems thus far has been to restate them in the formal statements of a computer language and let the processor solve it for me, usually by letting it test all possible answers and then reporting which one was correct. So I thought I would try something with more finesse for problem 9, which reads:

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`

.

I tried to find some way to write something elegant, but I gave up fairly quickly and wrote a brute-force solution so ugly it has a `goto`

in it.

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Problem9 { class Program { static void Main(string[] args) { int[] result = new int[3]; for (int i = 1; i < 800; i++) { for (int j = 1; j < 800; j++) { for (int k = 1; k < 800; k++) { if ((i + j + k == 1000) && (((j * j) + (i * i)) == (k * k))) { result[0] = i; result[1] = j; result[2] = k; goto Answer; } } } } Answer: Console.WriteLine(result[0] * result[1] * result[2]); } } }