From 7340ba9af757b6f135dff9c51a193ba527f061b3 Mon Sep 17 00:00:00 2001 From: eunhhu Date: Sun, 4 Oct 2026 13:58:19 +0100 Subject: Count with for i in a .. b, break, nest fors and let an event's own lists (Pure 018) --- examples/numbers.aup | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 examples/numbers.aup (limited to 'examples/numbers.aup') diff --git a/examples/numbers.aup b/examples/numbers.aup new file mode 100644 index 0000000..3352993 --- /dev/null +++ b/examples/numbers.aup @@ -0,0 +1,62 @@ +# Numbers worked out in one event (Pure 018): a square root by Newton's +# steps, stopping when they stop shrinking, and the best a knapsack holds, +# its table an event's list. +record Item { + id = 0 + weight = 0 + worth = 0 +} +record Cell { + at = 0 + best = 0 +} +state items: list of Item by id +state root = 0 +state steps = 0 +state packed = 0 +input stock = message { + id: u32 + weight: u32 + worth: u32 +} +input sqrt = message { + x: number +} +input pack = message { + capacity: u32 +} + +on stock { + items.add(Item { id: stock.id, weight: stock.weight, worth: stock.worth }) +} + +on sqrt { + let r = if sqrt.x > 1 { sqrt.x } else { 1 } + steps = 0 + for i in 0 .. 200 { + let next = (r + sqrt.x / r) / 2 + if next >= r { break } + r = next + steps = steps + 1 + } + root = r +} + +on pack { + let table: list of Cell by at + for c in 0 .. pack.capacity + 1 { + table.add(Cell { at: c }) + } + for it in items { + for k in 0 .. pack.capacity + 1 { + let c = pack.capacity - k + if c >= it.weight { + let with = table[c - it.weight].best + it.worth + if with > table[c].best { table[c].best = with } + } + } + } + packed = table[pack.capacity].best +} + +view answer = "root {root} in {steps} steps, packed {packed}" -- cgit v1.2.3