diff options
| author | _Tradam <[email protected]> | 2023-09-08 01:29:47 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-09-08 01:29:47 +0000 |
| commit | 3c76c7f3d5db3f9586a90d03f8fbb02d79de9acd (patch) | |
| tree | afbe4b540967223911f7c5de36559b82154f02f3 /misc/examples/triples.c | |
| parent | 0841165881871ee01b782129be681209aeed2423 (diff) | |
| parent | 1a72205fe05c2375cfd380dd8381a8460d9ed8d1 (diff) | |
| download | STC-modified-modified.tar.gz STC-modified-modified.zip | |
Diffstat (limited to 'misc/examples/triples.c')
| -rw-r--r-- | misc/examples/triples.c | 69 |
1 files changed, 0 insertions, 69 deletions
diff --git a/misc/examples/triples.c b/misc/examples/triples.c deleted file mode 100644 index 520bf012..00000000 --- a/misc/examples/triples.c +++ /dev/null @@ -1,69 +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 c = 5; n; ++c) { - for (int a = 1; a < c; ++a) { - for (int b = a + 1; b < c; ++b) { - if ((int64_t)a*a + (int64_t)b*b == (int64_t)c*c) { - printf("{%d, %d, %d}\n", a, b, c); - if (--n == 0) goto done; - } - } - } - } - done:; -} - -struct triples { - int n; - int a, b, c; - int cco_state; -}; - -bool triples_next(struct triples* I) { - cco_begin(I); - for (I->c = 5; I->n; ++I->c) { - for (I->a = 1; I->a < I->c; ++I->a) { - for (I->b = I->a + 1; I->b < I->c; ++I->b) { - if ((int64_t)I->a*I->a + (int64_t)I->b*I->b == (int64_t)I->c*I->c) { - cco_yield(true); - if (--I->n == 0) cco_return; - } - } - } - } - cco_final: - puts("done"); - cco_end(false); -} - -int gcd(int a, int b) { - while (b) { - int t = a % b; - a = b; - b = t; - } - return a; -} - -int main() -{ - puts("Vanilla triples:"); - triples_vanilla(6); - - puts("\nCoroutine triples:"); - struct triples t = {INT32_MAX}; - int n = 0; - - while (triples_next(&t)) { - if (gcd(t.a, t.b) > 1) - continue; - if (t.c < 100) - printf("%d: {%d, %d, %d}\n", ++n, t.a, t.b, t.c); - else - cco_stop(&t); - } -} |
