Skip to content

Instantly share code, notes, and snippets.

@michaeljclark
Created March 4, 2024 08:26
Show Gist options
  • Save michaeljclark/96b378046ed821fec3b7aaa42143597e to your computer and use it in GitHub Desktop.
Save michaeljclark/96b378046ed821fec3b7aaa42143597e to your computer and use it in GitHub Desktop.
find largest 32-bit Pythagorean integer triple with a 64-bit square
#include <stdio.h>
/*
* cc -O2 pythag.c -o pythag
*
* find largest 32-bit Pythagorean integer triple with a 64-bit square
*/
typedef unsigned long long ulong;
const ulong l = 65536;
ulong main(ulong argc, char **argv)
{
ulong lm, ln, a, b, c, la, lb, lc = 0;
for (ulong n = 1; n < l; n++)
{
for (ulong m = n+1; m < l; m++)
{
// Euclid's formula for Pythagorean triples
a = m*m-n*n;
b = 2*m*n;
c = m*m+n*n;
// where a^2 + b^2 = c^2
// save largest observed 32-bit c value
if (c < (1ull<<32) && c > lc) {
lm = m, ln = n, la = a, lb = b, lc = c;
}
}
}
printf("m=#%llx n=#%llx a=#%llx b=#%llx c=#%llx c^2=#%llx\n",
lm, ln, la, lb, lc, lc*lc);
}
./pythag
m=#f9ee n=#376b a=#e801b28b b=#6c3530f4 c=#fffffffd c^2=#fffffffa00000009
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment