summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/triples.c
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-03-07 22:26:36 +0100
committerTyge Løvset <[email protected]>2023-03-07 22:26:36 +0100
commitecc0b2108cffeb725e3b8e2574b6fb7927dfd96e (patch)
tree221a40b1e41f64353e2b3143277a250ef2ae2ce5 /misc/examples/triples.c
parenta203314647b5c37c7e40230551457f006ff36cd5 (diff)
downloadSTC-modified-ecc0b2108cffeb725e3b8e2574b6fb7927dfd96e.tar.gz
STC-modified-ecc0b2108cffeb725e3b8e2574b6fb7927dfd96e.zip
Improved/simplified c_forfilter (): last optional parameter gone. Now c_flt_take() and c_flt_takewhile() breaks the loop always.
c11/fmt.h : renamed fmt_freebuffer(buf) => fmt_destroy(buf).
Diffstat (limited to 'misc/examples/triples.c')
-rw-r--r--misc/examples/triples.c34
1 files changed, 18 insertions, 16 deletions
diff --git a/misc/examples/triples.c b/misc/examples/triples.c
index 8a46d653..4783d603 100644
--- a/misc/examples/triples.c
+++ b/misc/examples/triples.c
@@ -4,11 +4,11 @@
#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);
+ for (int i = 1, c = 1;; ++c) {
+ for (int a = 1; a < c; ++a) {
+ for (int b = a; b < c; ++b) {
+ if (a*a + b*b == c*c) {
+ printf("{%d, %d, %d},\n", a, b, c);
if (++i > n) goto done;
}
}
@@ -19,17 +19,17 @@ void triples_vanilla(int n) {
struct triples {
int n;
- int x, y, z;
+ int a, b, c;
int cco_state;
};
-bool triples_coro(struct triples* 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;
+bool triples_next(struct triples* I) {
+ cco_begin(I);
+ for (I->c = 1;; ++I->c) {
+ for (I->a = 1; I->a < I->c; ++I->a) {
+ for (I->b = I->a; I->b < I->c; ++I->b) {
+ if (I->a*I->a + I->b*I->b == I->c*I->c) {
+ if (I->n-- == 0) cco_return;
cco_yield(true);
}
}
@@ -46,7 +46,9 @@ int main()
triples_vanilla(6);
puts("\nCoroutine triples:");
- struct triples t = {6};
- while (triples_coro(&t))
- printf("{%d, %d, %d},\n", t.x, t.y, t.z);
+ struct triples t = {INT32_MAX};
+ while (triples_next(&t)) {
+ if (t.c < 100) printf("{%d, %d, %d},\n", t.a, t.b, t.c);
+ else cco_stop(&t);
+ }
}