expected cost
the maths you need
what it is
Expected cost is a method's cost averaged over every case, each times its chance. On average a shuffle of numbers has half its pairs out of order, and its new bests grow like the log of .
Say a method picks a whole number from 1 to 5, each as likely, then takes that many steps. Each has chance 1 over 5. The expected cost adds each cost times its chance: 1 over 5 + 2 over 5 and on to 5 over 5, 15 over 5, 3 steps.
With unequal chances the rule holds. Take 1 step with chance 3 over 4, or 2 with chance 1 over 4. That costs 1 times 3 over 4, plus 2 times 1 over 4: 5 over 4 on average.
A shuffle puts different numbers in an order picked at random, every order as likely. Count its pairs of places out of order, the bigger number first: 2, 3, 1 has two. Each pair is a piece of the count. For 3 numbers, each of the 3 pairs is out of order in half the 6 shuffles, so on average one half + one half + one half, 1.5.
Walk along a row and count its new bests, numbers bigger than all before them: 2 and 3 in 2, 3, 1. A method finding the biggest writes down each new best, a step each. A shuffle of numbers has of them on average, the harmonic sum: for 3, 11 over 6, about 1.83.
is read log to base 2 of . 2 × 2 × 2 × 2 is 16, four 2s, so the log of 16 is 4. 12 lies between 8 and 16, so, as the page halving and logarithms shows, its log lies between 3 and 4: about 3.58, by calculator.
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: each value times its chance
the line:times its chance 1 1 over 5 2 2 over 5 3 3 over 5 4 4 over 5 5 5 over 5 in all 15 over 5, which is 3 For 5, each number times its chance, 1 over 5. why it is allowed: The expected value adds each number times its chance, 1 over . , read the sum of, adds the term after it for each from the value under it to the value over it.
the move: take the chance out in front
the line:why it is allowed: Each term is multiplied by 1 over , so multiply once, after adding. For 5, 1 over 5 times 15.
guess it, thenthe move: use the page on adding up
the line:where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make? the move: draw the sum as a staircase
the line:why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
the move: take two staircases
the line:why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
the move: fit them into a rectangle
the line:Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15. why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
the move: halve both sides
the line:why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
why it is allowed: The whole numbers from 1 to add up to times one more than , halved, as the page on adding up 1 to works out. For 5, 5 times 6, halved, is 15.
guess it, thenthe move: cancel the n
the line:why it is allowed: on top and underneath cancel, as is not zero. For 5, 6 over 2 is 3.
guess it, thenthe move: read it
the line:why it is allowed: By line 4, halfway between the smallest number, 1, and the biggest, : 3 for 5.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: out of order half the time
the line:Trading places 1 and 3 pairs each shuffle holding them in order with one holding them out of order, 3 and 3. shuffle places 1 and 2 places 1 and 3 places 2 and 3 pairs out of order 1, 2, 3 0 1, 3, 2 yes 1 2, 1, 3 yes 1 2, 3, 1 yes yes 2 3, 1, 2 yes yes 2 3, 2, 1 yes yes yes 3 in all 3 3 3 9 Each shuffle of 1, 2 and 3, a yes where a pair of places is out of order. 9 over 6 is 1.5. why it is allowed: A pair is two places. Trading their numbers pairs each shuffle with another, and trading back undoes it. Each two has the pair out of order in exactly one, and both are as likely: chance one half.
the move: expected counts add
the line:why it is allowed: The table at line 1 adds two ways: down its last column, 9, and along its bottom row, 3 + 3 + 3. Over 6 shuffles, 9 over 6 is the average count, 1.5, and 3 over 6 three times is the pairs' chances added, 1.5 too. Both add the same yeses, so they agree for any , though the pairs depend on each other. With unequal chances, weight each yes by its order's chance. 2, 1, 3 at 3 over 4 and 1, 2, 3 at 1 over 4 give 3 over 4 both ways.
guess it, thenthe move: one half for each pair
the line:why it is allowed: By line 2 the expected count adds line 1's one half once per pair.
guess it, thenthe move: count the pairs
the line:why it is allowed: Let be the expected count, by line 3 half the number of pairs. The first place pairs with the after it, the next with one fewer, down to 1. For 3 places, 2 + 1 is 3 pairs, so 1.5.
guess it, thenthe move: add them the other way round
the line:why it is allowed: The same numbers in the other order.
guess it, thenthe move: use the page on adding up
the line:where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make? the move: draw the sum as a staircase
the line:why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
the move: take two staircases
the line:why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
the move: fit them into a rectangle
the line:Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15. why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
the move: halve both sides
the line:why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
why it is allowed: The page on adding up 1 to adds the numbers from 1 to any whole number. They add up to that number times one more than it, halved: 1 + 2 + 3 is 3 times 4, halved, which is 6. In its working, its sum runs from 1 up to its own last number, which it calls . Here that is .
guess it, thenthe move: tidy the bracket
the line:why it is allowed: is .
guess it, thenthe move: multiply the halves
the line:why it is allowed: Half of a half is a quarter. For 3, 2 times 3 over 4 is 1.5.
guess it, thenthe move: read it
the line:why it is allowed: By lines 4 to 7 there are pairs, and line 8 is half that. For 3, 1.5, as line 1 counted.
guess it, then
try it on small rows: over every order of 3 numbers, the average is
the move: a new best at place k
the line:Trading place 3 with place 1 or 2 moves the 3 there, and trading back undoes it. 2 shuffles hold the 3 at each place. shuffle place 1 place 2 place 3 new bests 1, 2, 3 yes yes yes 3 1, 3, 2 yes yes 2 2, 1, 3 yes yes 2 2, 3, 1 yes yes 2 3, 1, 2 yes 1 3, 2, 1 yes 1 in all 6 3 2 11 Each shuffle of 1, 2 and 3, a yes where a place holds a new best. 11 over 6 is 1 + 1 over 2 + 1 over 3. why it is allowed: The number at place is a new best when it is the biggest of the first . Trade place with another of the first : a biggest at one moves to the other, and trading back undoes it. So it stands equally often at each of the first places: at place in 1 of every shuffles. For 3, the 3 stands at each place in 2 of 6.
the move: add the chances
the line:why it is allowed: Let be the expected new bests. Each place is a piece, a new best or not. By line 2 of question 2, adds their chances: 1 over for place , by line 1.
guess it, thenthe move: read it
the line:why it is allowed: By line 2, the harmonic sum. For 3, 11 over 6, about 1.83, as line 1 counted.
guess it, then
try it on small rows: over every order of 3 numbers, the average is about 1.83
the move: name the sum
the line:why it is allowed: Let be the harmonic sum to 1 over , for from 2.
the move: group in blocks
the line:The sum to 1 over 12, about 3.10, each term a column 1 over k tall. Behind each place of a block stands its first term, dashed, so 4 dashed columns of 1 over 4 make 1. The last block is cut short. why it is allowed: Number blocks by from 0. Block starts at 1 over 2 to the power : 1, 1 over 2, 1 over 4, 1 over 8. It stops just before the next start, so it has 2 to the power terms: 1 over 4 to 1 over 7 are 4. The last block may be cut short at 1 over .
guess it, thenthe move: each block at most 1
the line:why it is allowed: Each term of block is at most its first, 1 over 2 to the power , as a bigger number underneath makes a smaller fraction. So its 2 to the power terms, or fewer, add to at most 1. is at most 1 per block: 4 for 12.
guess it, thenthe move: count the blocks
the line:where does this come from?
question 2 of the page halving and logarithmsHalve , rounding down each time. How many halvings reach 1? the move: name the log rounded down
the line:Rounding down, 11 halves to 5, 2 and 1. In blue, each log rounded down: 3, 2, 1, 0, one less at each halving. why it is allowed: Here is the number halved, from 2 up; for 11, is 3.
the move: rounding down never adds
the line:why it is allowed: For 11, 3 is at most 3.46.
the move: rounding down takes off less than 1
the line:why it is allowed: For 11, 4 is above 3.46.
the move: raise 2 to each side of line 2
the line:why it is allowed: 2 raised to a bigger power gives a bigger number. For 11, 8 is at most about 11.
the move: 2 to the log of n is n
the line:why it is allowed: By what a log is. For 11, 8 is at most 11.
the move: raise 2 to each side of line 3
the line:why it is allowed: As in line 4. For 11, about 11 is below 16.
the move: 2 to the log of n is n
the line:why it is allowed: As in line 5. For 11, 11 is below 16.
the move: halve both sides of line 5
the line:why it is allowed: For 11, 4 is at most 5.5, which is not whole.
the move: write the half as a power
the line:why it is allowed: Half a power of 2 has one 2 fewer. Half of 8 is 4.
the move: round the half down
the line:why it is allowed: is the half rounded down, 5 for 11. A whole number at most 5.5 is at most 5. The left side, 4 for 11, is whole, as is at least 1 for from 2 up.
the move: halve both sides of line 7
the line:why it is allowed: For 11, 5.5 is below 8.
the move: write the half as a power
the line:why it is allowed: As in line 9. Half of 16 is 8.
the move: round the half down
the line:why it is allowed: Rounding down never adds. For 11, 5 is below 8.
the move: take the log of line 10
the line:why it is allowed: A bigger number has a bigger log. For 11, the log of 4 is at most the log of 5.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, the log of 4 is 2, at most about 2.32.
the move: take the log of line 13
the line:why it is allowed: As in line 14. For 11, the log of 5 is below the log of 8.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, about 2.32 is below 3.
the move: round the log down
the line:the number at least below its log rounded down 11 8 16 3 5 4 8 2 2 2 4 1 1 1 2 0 Each halving takes 1 off the log rounded down. why it is allowed: By lines 15 and 17, the log of the half is at least one less than and below , so it rounds down to one less. For 11, about 2.32 rounds down to 2.
the move: use line 18 again and again
the line:why it is allowed: While the log rounded down is 1 or more, the number is at least 2 to the power 1, which is 2. So line 18 takes 1 off each halving, down to 0 after . For 11, 3, 2, 1, 0.
the move: a log rounded down to 0 belongs to 1
the line:why it is allowed: Such a log is at least 0 and below 1, so its number is at least 1 and below 2. It is 1.
why it is allowed: is read the log of rounded down. By lines 5 and 7 of the page halving and logarithms, 2 to that power is at most , and the next power of 2 is above . So blocks start at 1 over 2 to the powers 0 to . For 12, 3.58 rounds down to 3: the 4 blocks of line 2.
guess it, thenthe move: drop the rounding
the line:why it is allowed: Rounding down never adds, so putting for can never lower the right side. For 12, 4 becomes about 4.58.
guess it, thenthe move: a fact about n
the line:why it is allowed: is at least 2, the log of 2 is 1, and a bigger number has a bigger log.
guess it, thenthe move: put the log in place of the 1
the line:why it is allowed: By line 6 this makes the right side of line 5 no smaller. For 12, 4.58 becomes about 7.17.
guess it, thenthe move: add the logs
the line:why it is allowed: Two lots of .
guess it, thenthe move: a ceiling
the line:why it is allowed: By line 8, is at most 2 times for every from 2. , read is in big O of , says a fixed multiple of is a ceiling of : here 2.
guess it, thenthe move: blocks after the first term
the line:The sum to 1 over 12. Inside each term of a block stands its last term, dashed, so 2 dashed columns of 1 over 4 make one half. 1 over 9 to 1 over 12 are left over. why it is allowed: For a floor, group the terms after the first in blocks ending at 1 over 2, 1 over 4, 1 over 8 and on. Each term is then at least its block's last. Whole blocks end at 1 over the powers of 2 from 2 to .
guess it, thenthe move: drop the leftovers
the line:why it is allowed: Leftover terms are above zero, so leaving them out makes the total no bigger.
guess it, thenthe move: whole blocks at least one half
the line:why it is allowed: Number the whole blocks by from 1. Block runs from just after 1 over half of 2 to the power to 1 over 2 to the power . That is half that many terms, each at least its last, so at least one half. For 12, 1 over 3 + 1 over 4 is at least 2 times 1 over 4. With line 11, is at least 1 plus one half per whole block: 2.5.
guess it, thenthe move: count the whole blocks
the line:where does this come from?
question 2 of the page halving and logarithmsHalve , rounding down each time. How many halvings reach 1? the move: name the log rounded down
the line:Rounding down, 11 halves to 5, 2 and 1. In blue, each log rounded down: 3, 2, 1, 0, one less at each halving. why it is allowed: Here is the number halved, from 2 up; for 11, is 3.
the move: rounding down never adds
the line:why it is allowed: For 11, 3 is at most 3.46.
the move: rounding down takes off less than 1
the line:why it is allowed: For 11, 4 is above 3.46.
the move: raise 2 to each side of line 2
the line:why it is allowed: 2 raised to a bigger power gives a bigger number. For 11, 8 is at most about 11.
the move: 2 to the log of n is n
the line:why it is allowed: By what a log is. For 11, 8 is at most 11.
the move: raise 2 to each side of line 3
the line:why it is allowed: As in line 4. For 11, about 11 is below 16.
the move: 2 to the log of n is n
the line:why it is allowed: As in line 5. For 11, 11 is below 16.
the move: halve both sides of line 5
the line:why it is allowed: For 11, 4 is at most 5.5, which is not whole.
the move: write the half as a power
the line:why it is allowed: Half a power of 2 has one 2 fewer. Half of 8 is 4.
the move: round the half down
the line:why it is allowed: is the half rounded down, 5 for 11. A whole number at most 5.5 is at most 5. The left side, 4 for 11, is whole, as is at least 1 for from 2 up.
the move: halve both sides of line 7
the line:why it is allowed: For 11, 5.5 is below 8.
the move: write the half as a power
the line:why it is allowed: As in line 9. Half of 16 is 8.
the move: round the half down
the line:why it is allowed: Rounding down never adds. For 11, 5 is below 8.
the move: take the log of line 10
the line:why it is allowed: A bigger number has a bigger log. For 11, the log of 4 is at most the log of 5.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, the log of 4 is 2, at most about 2.32.
the move: take the log of line 13
the line:why it is allowed: As in line 14. For 11, the log of 5 is below the log of 8.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, about 2.32 is below 3.
the move: round the log down
the line:the number at least below its log rounded down 11 8 16 3 5 4 8 2 2 2 4 1 1 1 2 0 Each halving takes 1 off the log rounded down. why it is allowed: By lines 15 and 17, the log of the half is at least one less than and below , so it rounds down to one less. For 11, about 2.32 rounds down to 2.
the move: use line 18 again and again
the line:why it is allowed: While the log rounded down is 1 or more, the number is at least 2 to the power 1, which is 2. So line 18 takes 1 off each halving, down to 0 after . For 11, 3, 2, 1, 0.
the move: a log rounded down to 0 belongs to 1
the line:why it is allowed: Such a log is at least 0 and below 1, so its number is at least 1 and below 2. It is 1.
why it is allowed: As in line 4, whole blocks end at 1 over 2 to the powers 1 to , that many blocks. For 12, 3.
guess it, thenthe move: rounding takes off less than 1
the line:why it is allowed: Rounding down takes off less than 1, so putting less 1 for can only lower the right side. For 12, 2.5 becomes about 2.29.
guess it, thenthe move: halve each part
the line:why it is allowed: Half of ( less 1) is half of , less one half.
guess it, thenthe move: change the order
the line:why it is allowed: Adding and taking away go in any order.
guess it, thenthe move: 1 less one half
the line:why it is allowed: 1 less one half is one half.
guess it, thenthe move: drop the one half
the line:why it is allowed: Taking away one half makes the right side smaller. For 12, about 1.79.
guess it, thenthe move: a floor
the line:- the ceiling,
- the harmonic sum
- the floor,
From 2 terms on, the harmonic sum lies between its ceiling and floor. At 16: 8, about 3.38, and 2. why it is allowed: By line 18, is at least one half times for every from 2. , read is in big omega of , says a fixed multiple of , above zero, is a floor of .
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: , read is in big theta of , says both: by lines 9 and 19, for every from 2, lies between one half and 2 times . It grows like .
guess it, then
try it with a small : at , line 1 gives , about 3.1
a worked example
Take 1 to 4.
- Pick one, each as likely: 2.5 on average, by question 1.
- Shuffle them: 3 of the 6 pairs out of order on average, by question 2.
- Walk along a shuffle: 1 + 1 over 2 + 1 over 3 + 1 over 4, about 2.08 new bests, by question 3.
- The log of 4 is 2. 2.08 lies between half of it, 1, and twice it, 4, by question 4.
the trap
check yourself
What is the expected value of a whole number picked from 1 to 20, each as likely?
the answer
21 over 2, 10.5, by question 1. No pick gives it.
Shuffle 6 different numbers. How many pairs stand out of order, on average?
the answer
5 times 6 over 4, 7.5, by question 2, half the 15 pairs.
Is the harmonic sum up to 1 over 16 at least 3?
the answer
Yes. By line 13 of question 4, at least 1 plus half of 4, the log of 16. It is about 3.38.