Created
December 28, 2018 21:14
-
-
Save graemephi/0ccf818153dee9dacf828b576f185d80 to your computer and use it in GitHub Desktop.
This file contains 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
#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