summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/triples.c
diff options
context:
space:
mode:
authortylov <[email protected]>2023-07-20 15:09:10 +0200
committertylov <[email protected]>2023-07-20 15:12:29 +0200
commit900295256d825fc323149cd223c49787f32a3696 (patch)
tree6c79cf4209e3975bb6865e2940b9cb56ea469c73 /misc/examples/triples.c
parent224a04f7fa7549ed94d2a1415eb25829e39a7cca (diff)
downloadSTC-modified-900295256d825fc323149cd223c49787f32a3696.tar.gz
STC-modified-900295256d825fc323149cd223c49787f32a3696.zip
Moved examples to sub-directories. Added cotask1.c cotask2.c examples.
Diffstat (limited to 'misc/examples/triples.c')
-rw-r--r--misc/examples/triples.c72
1 files changed, 0 insertions, 72 deletions
diff --git a/misc/examples/triples.c b/misc/examples/triples.c
deleted file mode 100644
index 9f2fcc1e..00000000
--- a/misc/examples/triples.c
+++ /dev/null
@@ -1,72 +0,0 @@
-// https://quuxplusone.github.io/blog/2019/03/06/pythagorean-triples/
-
-#include <stc/algo/coroutine.h>
-#include <stdio.h>
-
-int gcd(int a, int b) {
- while (b) {
- int t = a % b;
- a = b;
- b = t;
- }
- return a;
-}
-
-void triples_vanilla(int n) {
- for (int c = 5, i = 0; 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 && gcd(a, b) == 1) {
- printf("%d: {%d, %d, %d}\n", ++i, a, b, c);
- if (--n == 0) goto done;
- }
- }
- }
- }
- done:;
-}
-
-struct triples {
- int size, count;
- int a, b, c;
- int cco_state;
-};
-
-int triples_coro(struct triples* t) {
- cco_routine(t) {
- t->count = 0;
- for (t->c = 5; t->size; ++t->c) {
- for (t->a = 1; t->a < t->c; ++t->a) {
- for (t->b = t->a + 1; t->b < t->c; ++t->b) {
- if ((int64_t)t->a*t->a + (int64_t)t->b*t->b == (int64_t)t->c*t->c) {
- if (t->count++ == t->size)
- cco_return;
- cco_yield();
- }
- }
- }
- }
- cco_cleanup:
- puts("done");
- }
- return 0;
-}
-
-int main(void)
-{
- puts("Vanilla triples:");
- triples_vanilla(5);
-
- puts("\nCoroutine triples:");
- struct triples t = {.size=INT32_MAX};
- int n = 0;
-
- while (triples_coro(&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);
- }
-}