‹ back to the board

Thursday, September 3

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 n is 0, the list is empty.
  • Otherwise the first entry is start.
  • Each following entry is derived from the previous entry p. Let d be the sum of the decimal digits of p. If p is even, the derived value is p / 2 + d (exact, since p is even). If p is odd, the derived value is p + d.
  • Entries keep being added until the list holds n entries, or until the derived value equals p — in that case the derived value is *not* added and the list ends there.
  • iterative sequence generation
  • Apprentice

examples

  1. case 1start 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
  2. case 2start of 7, n of 5[7, 14, 12, 9, 18] — the page fills before the wheel jams
  3. case 3start of 5, n of 0[] — the page holds nothing, so not even the first number is stamped
reward
220 XP
signature
tallyWheel
not yet run

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
The Miller's Tally Wheel · bitwick