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.
- held
- just put in
- copied into the new row, bracketed under it
- room to spare
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.
| put | cost | total | total ÷ puts |
|---|---|---|---|
| 1 | 1 | 1 | 1.00 |
| 2 | 2 | 3 | 1.50 |
| 3 | 3 | 6 | 2.00 |
| 4 | 1 | 7 | 1.75 |
| 5 | 5 | 12 | 2.40 |
| 6 | 1 | 13 | 2.17 |
| 7 | 1 | 14 | 2.00 |
| 8 | 1 | 15 | 1.88 |
| 9 | 9 | 24 | 2.67 |
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 puts, from 2 up, never cost more than . 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.
| put | cost | charged | bank |
|---|---|---|---|
| 1 | 1 | 3 | 2 |
| 2 | 2 | 3 | 3 |
| 3 | 3 | 3 | 3 |
| 4 | 1 | 3 | 5 |
| 5 | 5 | 3 | 3 |
| 6 | 1 | 3 | 5 |
| 7 | 1 | 3 | 7 |
| 8 | 1 | 3 | 9 |
| 9 | 9 | 3 | 3 |
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 , 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.
the move: writes and copies
the line:put that doubles copies room after 2 1 2 3 2 4 5 4 8 9 8 16 Every doubling of 9 puts. Each copies the room it finds full, and is 16. why it is allowed: Each put writes once, in all. The doublings copy the rooms 1, 2, 4 and on, each when it is full. The last doubling makes room , so it copies half of . For 9, 9 + 1 + 2 + 4 + 8 = 24.
the move: add the doublings
the line:where does this come from?
question 1 of the page doubling and halving sumsAdd . What is the total? the move: name the sum
the line:why it is allowed: Let be the doubling sum up to . For as 3, it is 1 + 2 + 4 + 8.
the move: double both sides
the line:why it is allowed: Each term doubled is the next one along, and becomes . Doubling both sides keeps them equal. The plan: the two sums share all but their ends.
the move: take line 1 away
the line:why it is allowed: Take from both sides of line 2. On the right, write out as line 1 does.
the move: tidy the left side
the line:why it is allowed: Two lots of less one lot is one lot.
the move: cancel what both brackets share
the line:when n is 3 1 2 4 8 16 first bracket 2 4 8 16 second bracket 1 2 4 8 left over take away 1 16 Line 4 when is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15. why it is allowed: Every term from 2 to is in both brackets and cancels, leaving less 1.
the move: the terms before the last
the line:why it is allowed: The sum of line 5 stopping one term sooner: line 5 with for . For 3, it is 1 + 2 + 4. At as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.
the move: tidy the power
the line:why it is allowed: One more than is . For 3, 1 + 2 + 4 is 7, one less than 8.
the move: read it
the line: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 , twice the last.
why it is allowed: 1 + 2 + 4 and on, up to half of , is one less than the next term, , by the doubling sum in the box under this line. In the box, counts something else. The room starts at 1 and only doubles, so is a power of 2. For 9, 1 + 2 + 4 + 8 = 15, which is 16 less 1.
guess it, thenthe move: half of p was full before a later put
the line:put held room 5 5 8 6 6 8 7 7 8 8 8 8 9 9 16 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 numbers filled the row, and one more put still came. So half of plus 1 is at most , and half of is at most . For 9, 8 + 1 = 9.
guess it, thenthe move: double both sides
the line:why it is allowed: Line 3, each side times 2. For 9, 16 is at most 16.
guess it, thenthe move: swap in the bigger number
the line:why it is allowed: Line 2, with swapped for , 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.
guess it, thenthe move: add up
the line:puts total at most 5 12 12 9 24 24 17 48 48 100 227 297 1,000 2,023 2,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: is , and taking 2 and then 1 takes 3. For 9, 24 is at most 24. For 100, 227 is at most 297.
guess it, thenthe move: spread over the puts
the line:why it is allowed: By line 6, the total is under 3 times the puts. Share it out, and each put gets under 3.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: the puts in between
the line:why it is allowed: The doubling before made room and left half of plus 1 in the row. Half of less 1 more puts fill it, and one more doubles it: half of puts in all. For room 8, put 5 made it, puts 6, 7 and 8 fill it, and put 9 doubles it: 4 puts.
the move: banked, less the copies
the line:why it is allowed: Each of those puts banks 2 coins, and the doubling copies numbers at 1 coin each. For room 8, 4 puts bank 8 coins, and 8 copies spend them.
guess it, thenthe move: halve and double
the line:why it is allowed: 2 lots of half of is .
guess it, thenthe move: nothing left over
the line:why it is allowed: less is 0. The puts since the last doubling pay for this one exactly.
guess it, thenthe move: never below 0
the line: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.
guess it, then
try it with a small : at , line 2 gives , 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.
- The row doubles when put 2, 3, 5, 9, 17 and 33 find it full.
- Those puts copy 1, 2, 4, 8, 16 and 32 numbers, 63 copies in all.
- Add the 33 writes: 33 + 63 = 96.
- Question 1 says at most 3 × 33 less 3, which is 96. Put 33 has just doubled the row, so the two are equal.
- Spread over the 33 puts, 96 is under 3 a put. Put 33 alone cost 33.
| put that doubles | copies | copies so far |
|---|---|---|
| 2 | 1 | 1 |
| 3 | 2 | 3 |
| 5 | 4 | 7 |
| 9 | 8 | 15 |
| 17 | 16 | 31 |
| 33 | 32 | 63 |
the trap
check yourself
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.
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.
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.