Performance errors
You have implemented some sort of a brute force solution which makes it extremely inefficient, this might work for a few more problems at Project Euler but pretty soon you will realize you need to put some thought in your code. There are few factors that damage your performance:
You have 3 for loops that makes your algorithm time complexity
which in most cases is avoidable.
The fact that you have a for loop for 'c' doesn't makes any sense. That suggests that you haven't run a debugger even once on your program or haven't payed attention to what's going on, because there are some obvious problems there even in your first iteration.
You have to manually check if aValue<bValue && bValue<cValue is true, if it was always true you would've had lesser iterations.
Your method IsPythagoreanTriplet is good as it is, but it's still not optimal because it requires you to know all 3 parameters, knowing 2 is enough.
Even after you find the correct number, you keep on iterating instead of breaking or instantly returning from your loop/method respectively. This will not only remove the extra if statements that you're unhappy with but will improve performance slightly.
Fixing the points above is pretty much equivalent to writing the program anew.
Let's start by writing a faster IsPythagoreanTriplet().
This is what the Pythagorean formula looks like:
.
We can make some points about how our program functions currently.
- 'a' and 'b' can be only integers.
- The desired sum is also an integer.
From that we can conclude that 'c' must also be an integer. Here is how the formula would look like if we were to find 'c':

In order for our conclusion to be correct,
must be a square number.
Following that logic we can write a simple method to determine if
is a square number and if that's the case than we have a triplet:
private static bool IsPythagoreanTriplet(long a, long b)
{
double rawResult = Math.Sqrt(a * a + b * b);
return rawResult == (long) rawResult;
}
That's alright but it would be also handy if we can somehow return the already calculated result for 'c' (if the numbers can form a triplet), just to avoid calculating it again in outside this method, we can do something like this:
private static bool IsPythagoreanTriplet(long a, long b, out long c)
{
double rawResult = Math.Sqrt(a * a + b * b);
long roundedResult = (long) rawResult;
if (rawResult == roundedResult)
{
c = roundedResult;
return true;
}
c = default(long);
return false;
}
That seems pretty good let's move on.
Faster and simpler GetProductOfPythagoreanTripletThatSumTo().
Goals:
- Use less than 3 loops.
- Avoid sets that don't make sense.
3.Get rid of the unnecessary parameter check (
aValue<bValue && bValue<cValue).
- Quit iterating once we got the desired result.
Fixing point 1 will also fix point 2, so let's start with that.
As I stated in my Performance errors section, I believe you would've noticed some weird sets appearing if you did ran a debugger, this is something that I highly emphasize on.
You are looking for 'c' with a for loop, while you can instantly obtain it using the formula. You're effectively looking for multiple solutions to the same operation, are there multiple answers to 2 + 2 = x? You don't start counting from 1 upwards and test every single number on your way, you simply add the two numbers to get the result.
Moving on to point 3. We can fix that by enforcing this rule in our for loops.
In order for this rule to be true we need to verify that:
b >= a + 1
Notice that this is our only constraint here, as we don't care about 'c' as we are going to form it as a sum of 'a' + 'b' which instantly makes it at least 'a' bigger than 'b'.
This is rather simple to implement as we are going to set this our base value in the for loop.
Lastly fixing point 4 is probably the easiest. We just gonna return value at the moment we find the correct result.
With all of that in mind we can write something like this:
private static long GetProductOfPythagoreanTripletThatSumTo(int desiredSum)
{
for (long a = 1; a < desiredSum / 2; a++)
{
for (long b = a + 1; b <= desiredSum / 2; b++)
{
long c;
if (IsPythagoreanTriplet(a, b, out c) && a + b + c == desiredSum)
{
return a * b * c;
}
}
}
return 0;
}