Project Euler Problem 9: Special Pythagorean triplet The problem statement is as follows:


  A Pythagorean triplet is a set of three natural numbers, a &lt; b &lt; 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.


My code that follows works. I dislike the if (valuesFound) ... break;. What is a better way of structuring this?

public class Program
{       
    public static void Main(string[] args)
    {   
        Console.WriteLine(GetProductOfPythagoreanTripletsThatSumTo(1000));
    } 


    static bool IsPythagoreanTriplet(int a, int b, int c)
    {
        return (a*a + b*b) == c*c;
    }

    static bool ValuesSumTo(int value, int a, int b, int c)
    {
        return value == a + b + c;
    }

    static long GetProductOfPythagoreanTripletsThatSumTo(int value)
    {
        int a=0;
        int b=0;
        int c=0;
        bool valuesFound = false;
        for (int aValue = 1; aValue &lt;=value/2; aValue++)
        {
            for (int bValue = 1; bValue&lt;=value/2; bValue++)
            { 
                for (int cValue= 1; cValue&lt;=value/2; cValue++)
                {
                    if (ValuesSumTo(value,aValue,bValue,cValue) &amp;&amp; IsPythagoreanTriplet(aValue,bValue,cValue) &amp;&amp; aValue&lt;bValue &amp;&amp; bValue&lt;cValue)
                    {
                        a = aValue;
                        b = bValue;
                        c = cValue;
                        valuesFound = true;
                    }
                }
                if (valuesFound)
                    break;
            }
            if (valuesFound)
                break;
        }
        return a * b * c;
    }
}
You may immediately

    return a * b * c;


as soon as the values are found.
The loop

    for (int cValue= 1; cValue&lt;=value/2; cValue++)


is superfluous: you already have a and b, and you know that \$a+b+c\$ must be equal to value, so

    cValue = value - aValue - bValue;


suffices.
Thou shalt not brute force.

Recall that the Pythagorean triple \$a, b, c: a^2 + b^2 = c^2\$ can be expressed as $$a = k(u^2 - v^2)\\
b = 2kuv\\
c = k(u^2 + v^2)$$
with coprime \$u,v\$. Since they must add up to a particular value \$N\$ (in your case, 1000), conclude that \$2k(u^2 + uv) = N\$, or \$v = \dfrac{N}{2ku} - u\$.


We know that sides must be positive, so we have 


\$u &gt; v \rightarrow u &gt; \dfrac{N}{2ku} - u \rightarrow u &gt; \sqrt{\dfrac{N}{4k}}\$, and 
\$v &gt; 0 \rightarrow \dfrac{N}{2ku} - u &gt; 0 \rightarrow u &lt; \sqrt{\dfrac{N}{2k}}\$

Now loop over the range [sqrt(125), sqrt(500)] looking or the divisors of 500 (the range is as small as [11 .. 22], and the only divisor is 20). I intentionally will not explain how to recover \$a, b, c\$ once the divisor is found. It is pretty straightforward.