Skip to content

Instantly share code, notes, and snippets.

@graemephi
Created December 28, 2018 21:14
Show Gist options
  • Save graemephi/0ccf818153dee9dacf828b576f185d80 to your computer and use it in GitHub Desktop.
Save graemephi/0ccf818153dee9dacf828b576f185d80 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdbool.h>
typedef struct TripleIterator
{
int iterations;
int x, y, z;
} TripleIterator;
bool next_triple(TripleIterator *it)
{
it->iterations++;
for (int z = it->z + 1; ; z++) {
for (int x = 1; x < z + 1; x++) {
for (int y = x; y < z + 1; y++) {
if ((x*x + y*y) == z*z) {
it->x = x;
it->y = y;
it->z = z;
return true;
}
}
}
}
return false;
}
int main(int argc, char **argv)
{
for (TripleIterator it = {0}; it.iterations < 10 && next_triple(&it);) {
printf("(%d, %d, %d)\n", it.x, it.y, it.z);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment