Last active
June 26, 2022 19:08
-
-
Save smrfeld/7b90e60e43b365a387fa4667931c87f7 to your computer and use it in GitHub Desktop.
Simplified Pythagorean triple
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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