C#: Project Euler Solution to Problem 39

http://projecteuler.net/problem=39

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p <= 1000, is the number of solutions maximised?

int Max_P = 0;
int P_position = 0;
for (int p = 1; p < 1000; p++)
{
    int count_p = 0;
    for (int a = 1; a < 500; a++)
    {
        for (int b = 1; b < 500; b++)
        {
            for (int c = 1; c < 500; c++)
            {
                if (  (a + b + c == p) &&
                (Math.Pow(a, 2.00) + Math.Pow(b, 2.00) == Math.Pow(c, 2.00)) )
                    count_p++;
            }
        }
    }
    if (Max_P < count_p)
    {
        Max_P = count_p;
        P_position = p;
    }
}
Console.WriteLine("p = "+P_position);

Leave a Reply

Your email address will not be published. Required fields are marked *