diff options
| author | Tyge Løvset <[email protected]> | 2023-03-01 14:36:38 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-03-01 14:36:38 +0100 |
| commit | 78a7e85535fd02e643bf98103223d4218e80133f (patch) | |
| tree | be22b46aaa7ac48b29125863d27db591cf12dcdf /misc/examples/triples.c | |
| parent | 8fdcfbf621b5e8c1298a89579594db0817adce26 (diff) | |
| download | STC-modified-78a7e85535fd02e643bf98103223d4218e80133f.tar.gz STC-modified-78a7e85535fd02e643bf98103223d4218e80133f.zip | |
Moved algorithm examples to algo folder.
Diffstat (limited to 'misc/examples/triples.c')
| -rw-r--r-- | misc/examples/triples.c | 52 |
1 files changed, 0 insertions, 52 deletions
diff --git a/misc/examples/triples.c b/misc/examples/triples.c deleted file mode 100644 index 0ce0eb37..00000000 --- a/misc/examples/triples.c +++ /dev/null @@ -1,52 +0,0 @@ -// https://quuxplusone.github.io/blog/2019/03/06/pythagorean-triples/ - -#include <stc/algo/coroutine.h> -#include <stdio.h> - -void triples_vanilla(int n) { - for (int i = 1, z = 1;; ++z) { - for (int x = 1; x < z; ++x) { - for (int y = x; y < z; ++y) { - if (x*x + y*y == z*z) { - printf("{%d, %d, %d},\n", x, y, z); - if (++i > n) goto done; - } - } - } - } - done:; -} - -struct tricoro { - int n; - int cco_state; - int x, y, z; -}; - -bool triples_coro(struct tricoro* t) { - cco_begin(t); - for (t->z = 1;; ++t->z) { - for (t->x = 1; t->x < t->z; ++t->x) { - for (t->y = t->x; t->y < t->z; ++t->y) { - if (t->x*t->x + t->y*t->y == t->z*t->z) { - if (t->n-- == 0) cco_return; - cco_yield(true); - } - } - } - } - cco_final: - cco_end(false); -} - - -int main() -{ - puts("Vanilla triples:"); - triples_vanilla(6); - - puts("\nCoroutine triples:"); - struct tricoro t = {6}; - while (triples_coro(&t)) - printf("{%d, %d, %d},\n", t.x, t.y, t.z); -}
\ No newline at end of file |
