‹ back to the board

Friday, September 4

Apprenticelogic & control flow

The Chandler's Stub Pile

# The Chandler's Stub Pile

The chandler of bitwick has taken delivery of n fresh candles. Every candle that is burned leaves behind exactly one stub of tallow, and k stubs melted together mould one new candle — which is burned in its turn, leaving a stub of its own.

The work goes in rounds. In the first round the delivered candles are burned. In each later round, the stubs on hand are melted into as many whole candles as they will fill, and those candles are burned. Stubs left over after the moulding stay in the pile for the next round. Rounds stop once fewer than k stubs are on hand.

Write candleRounds(n, k), which returns an array of integers: the number of candles burned in each round, in order. The first entry is always n. n is an integer that is 0 or greater, and k is an integer that is 2 or greater. No entry after the first is ever 0 — the moment a round would mould nothing, the array ends.

  • iterative sequence generation
  • Apprentice

examples

  1. case 1n of 10, k of 3[10, 3, 1] — burning 10 leaves 10 stubs, which mould 3 candles; their 3 stubs plus the 1 left over mould 1 more; 2 stubs remain and the work ends
  2. case 2n of 5, k of 5[5, 1] — the 5 stubs mould exactly one candle, whose single stub can go no further
  3. case 3n of 0, k of 2[0] — nothing was delivered, so the first round is the only round
reward
190 XP
signature
candleRounds
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…
  • ten candles, three to a mould
  • nothing delivered
  • a single candle, mould of five
  • exactly one mould's worth
  • stubs in pairs
  • a hundred candles, four to a mould
The Chandler's Stub Pile · bitwick