summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/generator.c
diff options
context:
space:
mode:
author_Tradam <[email protected]>2023-09-08 01:29:47 +0000
committerGitHub <[email protected]>2023-09-08 01:29:47 +0000
commit3c76c7f3d5db3f9586a90d03f8fbb02d79de9acd (patch)
treeafbe4b540967223911f7c5de36559b82154f02f3 /misc/examples/generator.c
parent0841165881871ee01b782129be681209aeed2423 (diff)
parent1a72205fe05c2375cfd380dd8381a8460d9ed8d1 (diff)
downloadSTC-modified-modified.tar.gz
STC-modified-modified.zip
Merge branch 'stclib:master' into modifiedHEADmodified
Diffstat (limited to 'misc/examples/generator.c')
-rw-r--r--misc/examples/generator.c53
1 files changed, 0 insertions, 53 deletions
diff --git a/misc/examples/generator.c b/misc/examples/generator.c
deleted file mode 100644
index 2bccc489..00000000
--- a/misc/examples/generator.c
+++ /dev/null
@@ -1,53 +0,0 @@
-// https://quuxplusone.github.io/blog/2019/03/06/pythagorean-triples/
-
-#include <stc/algo/coroutine.h>
-#include <stdio.h>
-
-typedef struct {
- int n;
- int a, b, c;
-} Triple_value, Triple;
-
-typedef struct {
- Triple_value* ref;
- int cco_state;
-} Triple_iter;
-
-bool Triple_next(Triple_iter* it) {
- Triple_value* t = it->ref;
- cco_begin(it);
- for (t->c = 1;; ++t->c) {
- for (t->a = 1; t->a < t->c; ++t->a) {
- for (t->b = t->a; t->b < t->c; ++t->b) {
- if (t->a*t->a + t->b*t->b == t->c*t->c) {
- if (t->n-- == 0) cco_return;
- cco_yield(true);
- }
- }
- }
- }
- cco_final:
- it->ref = NULL;
- cco_end(false);
-}
-
-Triple_iter Triple_begin(Triple* t) {
- Triple_iter it = {t};
- if (t->n > 0) Triple_next(&it);
- else it.ref = NULL;
- return it;
-}
-
-
-int main()
-{
- puts("Pythagorean triples with c < 100:");
- Triple t = {INT32_MAX};
- c_foreach (i, Triple, t)
- {
- if (i.ref->c < 100)
- printf("%u: (%d, %d, %d)\n", INT32_MAX - i.ref->n + 1, i.ref->a, i.ref->b, i.ref->c);
- else
- cco_stop(&i);
- }
-}