Skip to content

Instantly share code, notes, and snippets.

@smrfeld
Last active June 26, 2022 19:08
Show Gist options
  • Save smrfeld/7b90e60e43b365a387fa4667931c87f7 to your computer and use it in GitHub Desktop.
Save smrfeld/7b90e60e43b365a387fa4667931c87f7 to your computer and use it in GitHub Desktop.
Simplified Pythagorean triple
// Given int n
var soln = new List<(int a, int b, int c)>();
// Bound for a
double aMax = (1.0 - (1.0 / Math.Sqrt(2.0))) * n;
for (int a=1; a<aMax; a++)
{
// b = (N^2 - 2Na) / (2N - 2a)
int num = n * n - 2 * n * a;
int denom = 2 * n - 2 * a;
if (num % denom != 0)
{
continue;
}
int b = num / denom;
// c = N - a - b
int c = n - a - b;
soln.Add((a, b, c));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment