Skip to content

the cost spread over many steps (amortised cost)

counting the cost

what it is

Some steps cost a lot, but only now and then. Spread that cost over every step, and a row that doubles when full costs under 3 a put, however many you make.

Put numbers one at a time into a row with room for 1. When a put finds the row full, it first copies every number into a new row with twice the room, then writes its own. Count 1 for the write and 1 for each number copied.

copiedput 10put 21put 32put 40put 54put 60put 70put 80put 98Copies in all: 15.
  • held
  • just put in
  • copied into the new row, bracketed under it
  • room to spare
Nine puts into a row with room for 1. The row doubles at puts 2, 3, 5 and 9, copying 1, 2, 4 and 8 numbers.

Most puts cost 1. Put 5 finds 4 numbers and costs 5. Put 9 costs 9, and put 17 costs 17. So one put can cost a lot, and the costly ones grow.

putcosttotaltotal ÷ puts
1111.00
2231.50
3362.00
4171.75
55122.40
61132.17
71142.00
81151.88
99242.67
The first 9 puts. A doubling makes one put costly, and the total divided by the puts jumps, then falls back. It stays under 3.

Now add them all up. 9 puts cost 24 in all, under 3 a put, as the table shows. Question 1, in section 2, shows that nn puts, from 2 up, never cost more than 3n−33n - 3. Spread the total over every put, and each costs under 3. That share is called the amortised cost.

Here is another way to see it, from the books' volume two, chapter 25. Charge every put 3 coins. 1 pays for its write, and 2 go in a bank. When a put finds the row full, the bank pays for the copies. Question 2, in section 2, shows the bank always has enough, so 3 a put pays for everything.

putcostchargedbank
1132
2233
3333
4135
5533
6135
7137
8139
9933
Charging 3 a put. The bank grows between doublings and is back to 3 after each one. That 3 is what puts 1 and 2 left over, and the puts in between pay for each doubling exactly. It never goes below 0.

Amortised cost is the total of one run shared over its puts, and under 3 a put holds for every run of puts. Nothing here is random, so it is not an average over many runs. And it says nothing about one put alone. Put 1025 still copies 1024 numbers.

where the formulas come from

Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about nn, or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.

question 1What do nn puts cost in all, from 2 puts up? Let TT be the total, and pp the room after the last put. At 1 put, 3n−33n - 3 is 0, but the put costs 1, so 1 put is left out.
  1. the move: writes and copies

    the line:
    T=n+1+2+4+⋯+p2T = n + 1 + 2 + 4 + \dots + \frac{p}{2}
    put that doublescopiesroom after
    212
    324
    548
    9816
    Every doubling of 9 puts. Each copies the room it finds full, and pp is 16.

    why it is allowed: Each put writes once, nn in all. The doublings copy the rooms 1, 2, 4 and on, each when it is full. The last doubling makes room pp, so it copies half of pp. For 9, 9 + 1 + 2 + 4 + 8 = 24.

  2. the move: add the doublings

    the line:
    T=n+p−1T = n + p - 1
    where does this come from?
    question 1 of the page doubling and halving sumsAdd 1+2+4+⋯+2n1 + 2 + 4 + \dots + 2^n. What is the total?
    1. the move: name the sum

      the line:
      S=1+2+4+⋯+2nS = 1 + 2 + 4 + \dots + 2^n

      why it is allowed: Let SS be the doubling sum up to 2n2^n. For nn as 3, it is 1 + 2 + 4 + 8.

    2. the move: double both sides

      the line:
      2S=2+4+8+⋯+2n+12S = 2 + 4 + 8 + \dots + 2^{n + 1}

      why it is allowed: Each term doubled is the next one along, and 2n2^n becomes 2n+12^{n + 1}. Doubling both sides keeps them equal. The plan: the two sums share all but their ends.

    3. the move: take line 1 away

      the line:
      2S−S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} 2S - S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Take SS from both sides of line 2. On the right, write SS out as line 1 does.

    4. the move: tidy the left side

      the line:
      S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Two lots of SS less one lot is one lot.

    5. the move: cancel what both brackets share

      the line:
      S=2n+1−1S = 2^{n + 1} - 1
      when n is 3124816
      first bracket24816
      second bracket1248
      left overtake away 116
      Line 4 when nn is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15.

      why it is allowed: Every term from 2 to 2n2^n is in both brackets and cancels, leaving 2n+12^{n + 1} less 1.

    6. the move: the terms before the last

      the line:
      1+2+⋯+2n−1=2(n−1)+1−1\begin{gathered} 1 + 2 + \dots + 2^{n - 1} \\ = 2^{(n - 1) + 1} - 1 \end{gathered}

      why it is allowed: The sum of line 5 stopping one term sooner: line 5 with n−1n - 1 for nn. For 3, it is 1 + 2 + 4. At nn as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.

    7. the move: tidy the power

      the line:
      2(n−1)+1−1=2n−12^{(n - 1) + 1} - 1 = 2^n - 1

      why it is allowed: One more than n−1n - 1 is nn. For 3, 1 + 2 + 4 is 7, one less than 8.

    8. the move: read it

      the line:
      one less thanthe next term,under twice the last\begin{gathered} \text{one less than} \\ \text{the next term,} \\ \text{under twice the last} \end{gathered}

      why it is allowed: By lines 6 and 7, the terms before the last add to one less than it: the last outweighs them all. By line 5 the sum is under 2n+12^{n + 1}, twice the last.

    the whole page: doubling and halving sums

    why it is allowed: 1 + 2 + 4 and on, up to half of pp, is one less than the next term, pp, by the doubling sum in the box under this line. In the box, nn counts something else. The room starts at 1 and only doubles, so pp is a power of 2. For 9, 1 + 2 + 4 + 8 = 15, which is 16 less 1.

  3. the move: half of p was full before a later put

    the line:
    p2≤n−1\frac{p}{2} \leq n - 1
    putheldroom
    558
    668
    778
    888
    9916
    Puts 5 to 9. Room 8 is full after put 8, and put 9 doubles it to 16. So half of 16 is at most 9 less 1.

    why it is allowed: Half of pp numbers filled the row, and one more put still came. So half of pp plus 1 is at most nn, and half of pp is at most n−1n - 1. For 9, 8 + 1 = 9.

  4. the move: double both sides

    the line:
    p≤2n−2p \leq 2n - 2

    why it is allowed: Line 3, each side times 2. For 9, 16 is at most 16.

  5. the move: swap in the bigger number

    the line:
    T≤n+2n−2−1T \leq n + 2n - 2 - 1

    why it is allowed: Line 2, with pp swapped for 2n−22n - 2, which is at least as big by line 4. For 100, 100 + 128 less 1 is 227, at most 100 + 198 less 1, which is 297.

  6. the move: add up

    the line:
    T≤3n−3T \leq 3n - 3
    putstotalat most
    51212
    92424
    174848
    100227297
    1,0002,0232,997
    The total counted, against line 6. When the last put is one that doubled the row, the two are equal.

    why it is allowed: n+2nn + 2n is 3n3n, and taking 2 and then 1 takes 3. For 9, 24 is at most 24. For 100, 227 is at most 297.

  7. the move: spread over the puts

    the line:
    each put costs under 3,spread over the run\begin{gathered} \text{each put costs under 3,} \\ \text{spread over the run} \end{gathered}

    why it is allowed: By line 6, the total is under 3 times the puts. Share it out, and each put gets under 3.

try it with a small nn: at n=9n = 9, line 1 gives T=24T = 24, which you can work out by hand

question 2Charge 3 a put. Does the bank always pay for a doubling? Let mm be the room a doubling finds full. Let BB be what the puts since the doubling before add to the bank, less what this doubling spends.
  1. the move: the puts in between

    the line:
    m2 puts since thedoubling to room m\begin{gathered} \frac{m}{2} \text{ puts since the} \\ \text{doubling to room } m \end{gathered}

    why it is allowed: The doubling before made room mm and left half of mm plus 1 in the row. Half of mm less 1 more puts fill it, and one more doubles it: half of mm puts in all. For room 8, put 5 made it, puts 6, 7 and 8 fill it, and put 9 doubles it: 4 puts.

  2. the move: banked, less the copies

    the line:
    B=2×m2−mB = 2 \times \frac{m}{2} - m

    why it is allowed: Each of those puts banks 2 coins, and the doubling copies mm numbers at 1 coin each. For room 8, 4 puts bank 8 coins, and 8 copies spend them.

  3. the move: halve and double

    the line:
    B=m−mB = m - m

    why it is allowed: 2 lots of half of mm is mm.

  4. the move: nothing left over

    the line:
    B=0B = 0

    why it is allowed: mm less mm is 0. The puts since the last doubling pay for this one exactly.

  5. the move: never below 0

    the line:
    the bank neverfalls below 0\begin{gathered} \text{the bank never} \\ \text{falls below 0} \end{gathered}

    why it is allowed: The first doubling, at put 2, copies 1, and puts 1 and 2 banked 4, leaving 3. Each later doubling is paid for exactly by the puts since the one before, by line 4. Between doublings the bank only grows, so it never drops below the 3 that puts 1 and 2 left.

try it with a small mm: at m=8m = 8, line 2 gives B=0B = 0, which you can work out by hand

a worked example

Count 33 puts into a row with room for 1 by hand, and check them against question 1.

  1. The row doubles when put 2, 3, 5, 9, 17 and 33 find it full.
  2. Those puts copy 1, 2, 4, 8, 16 and 32 numbers, 63 copies in all.
  3. Add the 33 writes: 33 + 63 = 96.
  4. Question 1 says at most 3 × 33 less 3, which is 96. Put 33 has just doubled the row, so the two are equal.
  5. Spread over the 33 puts, 96 is under 3 a put. Put 33 alone cost 33.
put that doublescopiescopies so far
211
323
547
9815
171631
333263
Each doubling of 33 puts, and the copies added up. Each doubling copies more than all before it.

the trap

check yourself

  1. How many copies do 100 puts make into a row with room for 1, and what do the 100 puts cost in all?

    the answer

    The row doubles at puts 2, 3, 5, 9, 17, 33 and 65, copying 1 + 2 + 4 + 8 + 16 + 32 + 64 = 127. With the 100 writes, 227, under 3 a put.

  2. Charge only 2 coins a put. Where does the bank first fall below 0?

    the answer

    At put 5. The bank is 1, 1, 0 and 1 after puts 1 to 4. Put 5 costs 5 and pays 2, so the bank falls to 1 + 2 - 5 = -2.

  3. The total of a row's puts is under 3 a put. Does that mean no single put costs more than 3?

    the answer

    No. It is a bound on the whole run. Put 513 into a row with room for 1 costs 513, as it copies 512 numbers.