1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
= "composites"
[[]]
= "limit"
= "number"
[[]]
= "composites"
= "number"
# Compute max_divisor = floor(sqrt(limit))
[[]]
= "lib://flowstdlib/math/sqrt"
[[]]
= "input/limit"
= "sqrt"
########################################################################
# Inner loop: for current divisor, emit multiples from divisor*2..limit
#
# check-multiple(left=limit, right=multiple):
# right-lte: multiple <= limit → emit as composite
# right-gt: multiple > limit → (not used directly)
########################################################################
[[]]
= "check-multiple"
= "lib://flowstdlib/control/compare_switch"
# Seed with limit
[[]]
= "input/limit"
= "check-multiple/left"
# Seed with initial multiple = 2*2 = 4
[[]]
= "init-multiple"
= "lib://flowstdlib/math/multiply"
= { = 2}
= { = 2}
[[]]
= "init-multiple"
= "check-multiple/right"
# When multiple <= limit: output it as composite
[[]]
= "check-multiple/right-lte"
= "output/composites"
# Feedback limit while inner loop runs
[[]]
= "check-multiple/left-gt"
= "check-multiple/left"
########################################################################
# Compute next_multiple = current + divisor
########################################################################
[[]]
= "add-multiple"
= "lib://flowstdlib/math/add"
# i1 = current multiple (emitted composite)
[[]]
= "check-multiple/right-lte"
= "add-multiple/i1"
# i2 = current divisor (seeded from init, then from feedback)
[[]]
= "init-multiple/i1"
= "add-multiple/i2"
########################################################################
# Decide: continue inner loop or advance outer loop?
#
# compare-inner(left=limit, right=NEXT_multiple):
# gte: limit >= next_multiple → inner loop continues (inclusive)
# lt: limit < next_multiple → inner loop done, advance divisor
#
# Key: compare-inner receives the NEXT multiple (add-multiple output),
# not the current one. Uses gte so that next_multiple == limit is still
# fed back and emitted on the next iteration.
########################################################################
[[]]
= "compare-inner"
= "lib://flowstdlib/math/compare"
[[]]
= "input/limit"
= "compare-inner/left"
[[]]
= "add-multiple"
= "compare-inner/right"
# Feedback limit to compare-inner
[[]]
= "check-multiple/left-gt"
= "compare-inner/left"
########################################################################
# Inner loop feedback: route divisor and next_multiple based on decision
########################################################################
# divisor-route: routes the divisor based on compare-inner/gte
# true (continue) → loops divisor back to add-multiple/i2
# false (done) → sends divisor to add-divisor for outer loop
[[]]
= "divisor-route"
= "lib://flowstdlib/control/route"
[[]]
= "compare-inner/gte"
= "divisor-route/control"
[[]]
= "add-multiple/i2"
= "divisor-route/data"
# Continue inner: divisor loops back as step
[[]]
= "divisor-route/true"
= "add-multiple/i2"
# End inner: divisor goes to outer loop
[[]]
= "divisor-route/false"
= "add-divisor/i1"
# multiple-tap: gate next_multiple feedback when continuing inner loop
[[]]
= "multiple-tap"
= "lib://flowstdlib/control/tap"
[[]]
= "compare-inner/gte"
= "multiple-tap/control"
[[]]
= "add-multiple"
= "multiple-tap/data"
# Gated next_multiple feeds back to check-multiple for next inner iteration
[[]]
= "multiple-tap"
= "check-multiple/right"
########################################################################
# Outer loop: advance divisor when inner loop ends
########################################################################
# add-divisor: next_divisor = divisor + 1
[[]]
= "add-divisor"
= "lib://flowstdlib/math/add"
= { = 1}
# check-divisor(left=max_divisor, right=next_divisor):
# right-lte: restart inner loop with new divisor
# right-gt: done
[[]]
= "check-divisor"
= "lib://flowstdlib/control/compare_switch"
[[]]
= "sqrt"
= "check-divisor/left"
[[]]
= "add-divisor"
= "check-divisor/right"
# Feedback max_divisor for next outer iteration
[[]]
= "check-divisor/left-gt"
= "check-divisor/left"
# New starting multiple = next_divisor * 2
[[]]
= "new-start"
= "lib://flowstdlib/math/multiply"
= { = 2}
[[]]
= "check-divisor/right-lte"
= "new-start/i1"
# Feed new multiple into inner loop
[[]]
= "new-start"
= "check-multiple/right"
# Feed new divisor as step for inner loop
[[]]
= "check-divisor/right-lte"
= "add-multiple/i2"
# Feed limit back for the new inner loop
[[]]
= "check-divisor/left-gt"
= ["check-multiple/left", "compare-inner/left"]