Apprenticelogic & control flow
The Miller's Tally Wheel
The mill at the foot of bitwick keeps its count on a brass tally wheel. Turning the crank stamps one number into the ledger, each worked out from the number stamped just before it. The wheel jams when the number it is about to stamp matches the one before it, and nothing further goes into the ledger.
The miller wants the ledger page written out.
Arguments
start— a non-negative integer: the first number stamped.n— a non-negative integer: the most entries the page can hold.
What `tallyWheel(start, n)` returns
A list of integers: the ledger entries, in order.
- If
nis0, the list is empty. - Otherwise the first entry is
start. - Each following entry is derived from the previous entry
p. Letdbe the sum of the decimal digits ofp. Ifpis even, the derived value isp / 2 + d(exact, sincepis even). Ifpis odd, the derived value isp + d. - Entries keep being added until the list holds
nentries, or until the derived value equalsp— in that case the derived value is *not* added and the list ends there.
examples
- case 1
start of 12, n of 6[12, 9, 18] — 12 is even, so 6 + 3 = 9; 9 is odd, so 9 + 9 = 18; 18 is even, so 9 + 9 = 18 again, which matches, so the wheel jams before a sixth entry - case 2
start of 7, n of 5[7, 14, 12, 9, 18] — the page fills before the wheel jams - case 3
start of 5, n of 0[] — the page holds nothing, so not even the first number is stamped
- reward
- 220 XP
- signature
tallyWheel
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
loading the editor…
- jams at eighteen
- page fills first
- long climb from one
- starts jammed
- zero stamps once
- empty page
- three digit start
- hundred
- single entry page