Skip to content

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 nn numbers has half its pairs out of order, and its new bests grow like the log of nn.

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.

1 to 5 steps as blue columns, a yellow copy hung over them. Each column holds 6, 30 in all. Half, 15, are blue, so the blue average 3.

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.

arcsarcs1230132121312312312232139 arcs in 6 shuffles
The 6 shuffles of 1, 2 and 3, an arc over each pair out of order. Each pair of places has an arc in 3 of them, so 9 arcs, 1.5 a shuffle.

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 nn numbers has 1+12+13+⋯+1n1 + \frac{1}{2} + \frac{1}{3} + \dots + \frac{1}{n} of them on average, the harmonic sum: for 3, 11 over 6, about 1.83.

log⁡2 n\log_2\, n is read log to base 2 of nn. 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.

numberlog× 2+ 110214283164123.58
One step right doubles the number and adds 1 to its log. 12 lies between 8 and 16, its log about 3.58.

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 1Pick a whole number from 1 to nn, each as likely. What is its expected value?
  1. the move: each value times its chance

    the line:
    E=∑k=1nk×1nE = \sum_{k=1}^{n} k \times \frac{1}{n}
    kktimes its chance
    11 over 5
    22 over 5
    33 over 5
    44 over 5
    55 over 5
    in all15 over 5, which is 3
    For 5, each number times its chance, 1 over 5.

    why it is allowed: The expected value EE adds each number kk times its chance, 1 over nn. Σ\Sigma, read the sum of, adds the term after it for each kk from the value under it to the value over it.

  2. the move: take the chance out in front

    the line:
    E=1n∑k=1nkE = \frac{1}{n} \sum_{k=1}^{n} k

    why it is allowed: Each term is multiplied by 1 over nn, so multiply once, after adding. For 5, 1 over 5 times 15.

  3. the move: use the page on adding up

    the line:
    E=1n×n(n+1)2E = \frac{1}{n} \times \frac{n(n + 1)}{2}
    where does this come from?
    question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
    1. the move: draw the sum as a staircase

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

      why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

    2. the move: take two staircases

      the line:
      2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

      why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

    3. the move: fit them into a rectangle

      the line:
      2S=n(n+1)2S = n(n + 1)
      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 nn 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 n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

    4. the move: halve both sides

      the line:
      S=n(n+1)2S = \frac{n(n + 1)}{2}

      why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

    the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

    why it is allowed: The whole numbers from 1 to nn add up to nn times one more than nn, halved, as the page on adding up 1 to nn works out. For 5, 5 times 6, halved, is 15.

  4. the move: cancel the n

    the line:
    E=n+12E = \frac{n + 1}{2}

    why it is allowed: nn on top and nn underneath cancel, as nn is not zero. For 5, 6 over 2 is 3.

  5. the move: read it

    the line:
    the middle of1 and n\begin{gathered} \text{the middle of} \\ \text{1 and } n \end{gathered}

    why it is allowed: By line 4, halfway between the smallest number, 1, and the biggest, nn: 3 for 5.

try it with a small nn: at n=5n = 5, line 1 gives E=3E = 3, which you can work out by hand

question 2Shuffle nn different numbers at random. How many pairs stand out of order, on average?
  1. the move: out of order half the time

    the line:
    a pair stands out of orderwith chance one half\begin{gathered} \text{a pair stands out of order} \\ \text{with chance one half} \end{gathered}
    in order1231322133 shufflesout of order3212313123 shuffles=
    Trading places 1 and 3 pairs each shuffle holding them in order with one holding them out of order, 3 and 3.
    shuffleplaces 1 and 2places 1 and 3places 2 and 3pairs out of order
    1, 2, 30
    1, 3, 2yes1
    2, 1, 3yes1
    2, 3, 1yesyes2
    3, 1, 2yesyes2
    3, 2, 1yesyesyes3
    in all3339
    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.

  2. the move: expected counts add

    the line:
    the expected countadds up the chanceof every pair\begin{gathered} \text{the expected count} \\ \text{adds up the chance} \\ \text{of every pair} \end{gathered}

    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 nn, 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.

  3. the move: one half for each pair

    the line:
    one half timesthe number of pairs\begin{gathered} \text{one half times} \\ \text{the number of pairs} \end{gathered}

    why it is allowed: By line 2 the expected count adds line 1's one half once per pair.

  4. the move: count the pairs

    the line:
    P=12×((n−1)+⋯+1)P = \frac{1}{2} \times \bigl((n - 1) + \dots + 1\bigr)

    why it is allowed: Let PP be the expected count, by line 3 half the number of pairs. The first place pairs with the n−1n - 1 after it, the next with one fewer, down to 1. For 3 places, 2 + 1 is 3 pairs, so 1.5.

  5. the move: add them the other way round

    the line:
    P=12×(1+⋯+(n−1))P = \frac{1}{2} \times \bigl(1 + \dots + (n - 1)\bigr)

    why it is allowed: The same numbers in the other order.

  6. the move: use the page on adding up

    the line:
    P=12×(n−1)((n−1)+1)2P = \frac{1}{2} \times \frac{(n - 1)\bigl((n - 1) + 1\bigr)}{2}
    where does this come from?
    question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
    1. the move: draw the sum as a staircase

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

      why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

    2. the move: take two staircases

      the line:
      2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

      why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

    3. the move: fit them into a rectangle

      the line:
      2S=n(n+1)2S = n(n + 1)
      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 nn 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 n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

    4. the move: halve both sides

      the line:
      S=n(n+1)2S = \frac{n(n + 1)}{2}

      why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

    the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

    why it is allowed: The page on adding up 1 to nn 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 nn. Here that is n−1n - 1.

  7. the move: tidy the bracket

    the line:
    P=12×(n−1)n2P = \frac{1}{2} \times \frac{(n - 1)n}{2}

    why it is allowed: (n−1)+1(n - 1) + 1 is nn.

  8. the move: multiply the halves

    the line:
    P=(n−1)n4P = \frac{(n - 1)n}{4}

    why it is allowed: Half of a half is a quarter. For 3, 2 times 3 over 4 is 1.5.

  9. the move: read it

    the line:
    half the pairs,on average\begin{gathered} \text{half the pairs,} \\ \text{on average} \end{gathered}

    why it is allowed: By lines 4 to 7 there are (n−1)n2\frac{(n - 1)n}{2} pairs, and line 8 is half that. For 3, 1.5, as line 1 counted.

try it on small rows: over every order of 3 numbers, the average is P=1.5P = 1.5

question 3Walk along nn different numbers shuffled at random. How many new bests turn up, on average?
  1. the move: a new best at place k

    the line:
    place k holds a new bestwith chance 1 over k\begin{gathered} \text{place } k \text{ holds a new best} \\ \text{with chance 1 over } k \end{gathered}
    3 at place 13213122 shuffles3 at place 31232132 shuffles=3 at place 21322312 shuffles=
    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.
    shuffleplace 1place 2place 3new bests
    1, 2, 3yesyesyes3
    1, 3, 2yesyes2
    2, 1, 3yesyes2
    2, 3, 1yesyes2
    3, 1, 2yes1
    3, 2, 1yes1
    in all63211
    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 kk is a new best when it is the biggest of the first kk. Trade place kk with another of the first kk: a biggest at one moves to the other, and trading back undoes it. So it stands equally often at each of the first kk places: at place kk in 1 of every kk shuffles. For 3, the 3 stands at each place in 2 of 6.

  2. the move: add the chances

    the line:
    R=1+12+13+⋯+1nR = 1 + \frac{1}{2} + \frac{1}{3} + \dots + \frac{1}{n}

    why it is allowed: Let RR be the expected new bests. Each place is a piece, a new best or not. By line 2 of question 2, RR adds their chances: 1 over kk for place kk, by line 1.

  3. the move: read it

    the line:
    the harmonic sum\begin{gathered} \text{the harmonic sum} \end{gathered}

    why it is allowed: By line 2, the harmonic sum. For 3, 11 over 6, about 1.83, as line 1 counted.

try it on small rows: over every order of 3 numbers, the average RR is about 1.83

question 4How does 1+12+13+⋯+1n1 + \frac{1}{2} + \frac{1}{3} + \dots + \frac{1}{n} grow?
  1. the move: name the sum

    the line:
    H=∑k=1n1kH = \sum_{k=1}^{n} \frac{1}{k}

    why it is allowed: Let HH be the harmonic sum to 1 over nn, for nn from 2.

  2. the move: group in blocks

    the line:
    blocks, each starting at1 over a power of 2\begin{gathered} \text{blocks, each starting at} \\ \text{1 over a power of 2} \end{gathered}
    blocktermsadds to01≤ 112≤ 124≤ 135≤ 1
    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 jj from 0. Block jj starts at 1 over 2 to the power jj: 1, 1 over 2, 1 over 4, 1 over 8. It stops just before the next start, so it has 2 to the power jj terms: 1 over 4 to 1 over 7 are 4. The last block may be cut short at 1 over nn.

  3. the move: each block at most 1

    the line:
    H≤the number of blocksH \le \text{the number of blocks}

    why it is allowed: Each term of block jj is at most its first, 1 over 2 to the power jj, as a bigger number underneath makes a smaller fraction. So its 2 to the power jj terms, or fewer, add to at most 1. HH is at most 1 per block: 4 for 12.

  4. the move: count the blocks

    the line:
    H≤⌊log⁡2 n⌋+1H \le \lfloor \log_2\, n \rfloor + 1
    where does this come from?
    question 2 of the page halving and logarithmsHalve nn, rounding down each time. How many halvings reach 1?
    1. the move: name the log rounded down

      the line:
      K=⌊log⁡2 n⌋K = \lfloor \log_2\, n \rfloor
      numberlog× 2+ 14283164113.46352.322211100
      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 nn is the number halved, from 2 up; for 11, KK is 3.

    2. the move: rounding down never adds

      the line:
      ⌊log⁡2 n⌋≤log⁡2 n\lfloor \log_2\, n \rfloor \le \log_2\, n

      why it is allowed: For 11, 3 is at most 3.46.

    3. the move: rounding down takes off less than 1

      the line:
       log⁡2 n<⌊log⁡2 n⌋+1\begin{gathered} \,\log_2\, n \\ < \lfloor \log_2\, n \rfloor + 1 \end{gathered}

      why it is allowed: For 11, 4 is above 3.46.

    4. the move: raise 2 to each side of line 2

      the line:
      2⌊log⁡2 n⌋≤2log⁡2 n2^{\lfloor \log_2\, n \rfloor} \le 2^{\log_2\, n}

      why it is allowed: 2 raised to a bigger power gives a bigger number. For 11, 8 is at most about 11.

    5. the move: 2 to the log of n is n

      the line:
      2⌊log⁡2 n⌋≤n2^{\lfloor \log_2\, n \rfloor} \le n

      why it is allowed: By what a log is. For 11, 8 is at most 11.

    6. the move: raise 2 to each side of line 3

      the line:
      2log⁡2 n<2⌊log⁡2 n⌋+12^{\log_2\, n} < 2^{\lfloor \log_2\, n \rfloor + 1}

      why it is allowed: As in line 4. For 11, about 11 is below 16.

    7. the move: 2 to the log of n is n

      the line:
      n<2⌊log⁡2 n⌋+1n < 2^{\lfloor \log_2\, n \rfloor + 1}

      why it is allowed: As in line 5. For 11, 11 is below 16.

    8. the move: halve both sides of line 5

      the line:
      2⌊log⁡2 n⌋/2≤n/22^{\lfloor \log_2\, n \rfloor} / 2 \le n / 2

      why it is allowed: For 11, 4 is at most 5.5, which is not whole.

    9. the move: write the half as a power

      the line:
      2⌊log⁡2 n⌋−1≤n/22^{\lfloor \log_2\, n \rfloor - 1} \le n / 2

      why it is allowed: Half a power of 2 has one 2 fewer. Half of 8 is 4.

    10. the move: round the half down

      the line:
      2⌊log⁡2 n⌋−1≤⌊n/2⌋2^{\lfloor \log_2\, n \rfloor - 1} \le \lfloor n / 2 \rfloor

      why it is allowed: ⌊n/2⌋\lfloor n / 2 \rfloor 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 KK is at least 1 for nn from 2 up.

    11. the move: halve both sides of line 7

      the line:
      n/2<2⌊log⁡2 n⌋+1/2n / 2 < 2^{\lfloor \log_2\, n \rfloor + 1} / 2

      why it is allowed: For 11, 5.5 is below 8.

    12. the move: write the half as a power

      the line:
      n/2<2⌊log⁡2 n⌋n / 2 < 2^{\lfloor \log_2\, n \rfloor}

      why it is allowed: As in line 9. Half of 16 is 8.

    13. the move: round the half down

      the line:
      ⌊n/2⌋<2⌊log⁡2 n⌋\lfloor n / 2 \rfloor < 2^{\lfloor \log_2\, n \rfloor}

      why it is allowed: Rounding down never adds. For 11, 5 is below 8.

    14. the move: take the log of line 10

      the line:
       log⁡2 2⌊log⁡2 n⌋−1≤log⁡2 ⌊n/2⌋\begin{gathered} \,\log_2\, 2^{\lfloor \log_2\, n \rfloor - 1} \\ \le \log_2\, \lfloor n / 2 \rfloor \end{gathered}

      why it is allowed: A bigger number has a bigger log. For 11, the log of 4 is at most the log of 5.

    15. the move: the log of a power of 2 is the power

      the line:
      ⌊log⁡2 n⌋−1≤log⁡2 ⌊n/2⌋\begin{gathered} \lfloor \log_2\, n \rfloor - 1 \\ \le \log_2\, \lfloor n / 2 \rfloor \end{gathered}

      why it is allowed: For 11, the log of 4 is 2, at most about 2.32.

    16. the move: take the log of line 13

      the line:
       log⁡2 ⌊n/2⌋<log⁡2 2⌊log⁡2 n⌋\begin{gathered} \,\log_2\, \lfloor n / 2 \rfloor \\ < \log_2\, 2^{\lfloor \log_2\, n \rfloor} \end{gathered}

      why it is allowed: As in line 14. For 11, the log of 5 is below the log of 8.

    17. the move: the log of a power of 2 is the power

      the line:
       log⁡2 ⌊n/2⌋<⌊log⁡2 n⌋\begin{gathered} \,\log_2\, \lfloor n / 2 \rfloor \\ < \lfloor \log_2\, n \rfloor \end{gathered}

      why it is allowed: For 11, about 2.32 is below 3.

    18. the move: round the log down

      the line:
      ⌊log⁡2 ⌊n/2⌋⌋=⌊log⁡2 n⌋−1\begin{gathered} \lfloor \log_2\, \lfloor n / 2 \rfloor \rfloor \\ = \lfloor \log_2\, n \rfloor - 1 \end{gathered}
      the numberat leastbelowits log rounded down
      118163
      5482
      2241
      1120
      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 KK and below KK, so it rounds down to one less. For 11, about 2.32 rounds down to 2.

    19. the move: use line 18 again and again

      the line:
      after K halvings,rounding down,the log roundeddown is 0\begin{gathered} \text{after } K \text{ halvings,} \\ \text{rounding down,} \\ \text{the log rounded} \\ \text{down is 0} \end{gathered}

      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 KK. For 11, 3, 2, 1, 0.

    20. the move: a log rounded down to 0 belongs to 1

      the line:
      K halvings,rounding down,bring n to 1\begin{gathered} K \text{ halvings,} \\ \text{rounding down,} \\ \text{bring } n \text{ to 1} \end{gathered}

      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.

    the whole page: halving and logarithms

    why it is allowed: ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor is read the log of nn rounded down. By lines 5 and 7 of the page halving and logarithms, 2 to that power is at most nn, and the next power of 2 is above nn. So ⌊log⁡2 n⌋+1\lfloor \log_2\, n \rfloor + 1 blocks start at 1 over 2 to the powers 0 to ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor. For 12, 3.58 rounds down to 3: the 4 blocks of line 2.

  5. the move: drop the rounding

    the line:
    H≤log⁡2 n+1H \le \log_2\, n + 1

    why it is allowed: Rounding down never adds, so putting log⁡2 n\log_2\, n for ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor can never lower the right side. For 12, 4 becomes about 4.58.

  6. the move: a fact about n

    the line:
    1≤log⁡2 n1 \le \log_2\, n

    why it is allowed: nn is at least 2, the log of 2 is 1, and a bigger number has a bigger log.

  7. the move: put the log in place of the 1

    the line:
    H≤log⁡2 n+log⁡2 nH \le \log_2\, n + \log_2\, n

    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.

  8. the move: add the logs

    the line:
    H≤2 log⁡2 nH \le 2 \,\log_2\, n

    why it is allowed: Two lots of log⁡2 n\log_2\, n.

  9. the move: a ceiling

    the line:
    H∈O(log⁡2 n)H \in O(\log_2\, n)

    why it is allowed: By line 8, HH is at most 2 times log⁡2 n\log_2\, n for every nn from 2. H∈O(log⁡2 n)H \in O(\log_2\, n), read HH is in big O of log⁡2 n\log_2\, n, says a fixed multiple of log⁡2 n\log_2\, n is a ceiling of HH: here 2.

  10. the move: blocks after the first term

    the line:
    after the first term,blocks, each ending at1 over a power of 2\begin{gathered} \text{after the first term,} \\ \text{blocks, each ending at} \\ \text{1 over a power of 2} \end{gathered}
    blocktermsadds tofirst11≥ ½22≥ ½34≥ ½left over
    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 nn.

  11. the move: drop the leftovers

    the line:
    H is at least 1 plusthe whole blocksadded up\begin{gathered} H \text{ is at least 1 plus} \\ \text{the whole blocks} \\ \text{added up} \end{gathered}

    why it is allowed: Leftover terms are above zero, so leaving them out makes the total no bigger.

  12. the move: whole blocks at least one half

    the line:
    H≥1+12×the numberof whole blocks\begin{gathered} H \ge 1 + \frac{1}{2} \times \text{the number} \\ \text{of whole blocks} \end{gathered}

    why it is allowed: Number the whole blocks by ii from 1. Block ii runs from just after 1 over half of 2 to the power ii to 1 over 2 to the power ii. 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, HH is at least 1 plus one half per whole block: 2.5.

  13. the move: count the whole blocks

    the line:
    H≥1+⌊log⁡2 n⌋2H \ge 1 + \frac{\lfloor \log_2\, n \rfloor}{2}
    where does this come from?
    question 2 of the page halving and logarithmsHalve nn, rounding down each time. How many halvings reach 1?
    1. the move: name the log rounded down

      the line:
      K=⌊log⁡2 n⌋K = \lfloor \log_2\, n \rfloor
      numberlog× 2+ 14283164113.46352.322211100
      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 nn is the number halved, from 2 up; for 11, KK is 3.

    2. the move: rounding down never adds

      the line:
      ⌊log⁡2 n⌋≤log⁡2 n\lfloor \log_2\, n \rfloor \le \log_2\, n

      why it is allowed: For 11, 3 is at most 3.46.

    3. the move: rounding down takes off less than 1

      the line:
       log⁡2 n<⌊log⁡2 n⌋+1\begin{gathered} \,\log_2\, n \\ < \lfloor \log_2\, n \rfloor + 1 \end{gathered}

      why it is allowed: For 11, 4 is above 3.46.

    4. the move: raise 2 to each side of line 2

      the line:
      2⌊log⁡2 n⌋≤2log⁡2 n2^{\lfloor \log_2\, n \rfloor} \le 2^{\log_2\, n}

      why it is allowed: 2 raised to a bigger power gives a bigger number. For 11, 8 is at most about 11.

    5. the move: 2 to the log of n is n

      the line:
      2⌊log⁡2 n⌋≤n2^{\lfloor \log_2\, n \rfloor} \le n

      why it is allowed: By what a log is. For 11, 8 is at most 11.

    6. the move: raise 2 to each side of line 3

      the line:
      2log⁡2 n<2⌊log⁡2 n⌋+12^{\log_2\, n} < 2^{\lfloor \log_2\, n \rfloor + 1}

      why it is allowed: As in line 4. For 11, about 11 is below 16.

    7. the move: 2 to the log of n is n

      the line:
      n<2⌊log⁡2 n⌋+1n < 2^{\lfloor \log_2\, n \rfloor + 1}

      why it is allowed: As in line 5. For 11, 11 is below 16.

    8. the move: halve both sides of line 5

      the line:
      2⌊log⁡2 n⌋/2≤n/22^{\lfloor \log_2\, n \rfloor} / 2 \le n / 2

      why it is allowed: For 11, 4 is at most 5.5, which is not whole.

    9. the move: write the half as a power

      the line:
      2⌊log⁡2 n⌋−1≤n/22^{\lfloor \log_2\, n \rfloor - 1} \le n / 2

      why it is allowed: Half a power of 2 has one 2 fewer. Half of 8 is 4.

    10. the move: round the half down

      the line:
      2⌊log⁡2 n⌋−1≤⌊n/2⌋2^{\lfloor \log_2\, n \rfloor - 1} \le \lfloor n / 2 \rfloor

      why it is allowed: ⌊n/2⌋\lfloor n / 2 \rfloor 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 KK is at least 1 for nn from 2 up.

    11. the move: halve both sides of line 7

      the line:
      n/2<2⌊log⁡2 n⌋+1/2n / 2 < 2^{\lfloor \log_2\, n \rfloor + 1} / 2

      why it is allowed: For 11, 5.5 is below 8.

    12. the move: write the half as a power

      the line:
      n/2<2⌊log⁡2 n⌋n / 2 < 2^{\lfloor \log_2\, n \rfloor}

      why it is allowed: As in line 9. Half of 16 is 8.

    13. the move: round the half down

      the line:
      ⌊n/2⌋<2⌊log⁡2 n⌋\lfloor n / 2 \rfloor < 2^{\lfloor \log_2\, n \rfloor}

      why it is allowed: Rounding down never adds. For 11, 5 is below 8.

    14. the move: take the log of line 10

      the line:
       log⁡2 2⌊log⁡2 n⌋−1≤log⁡2 ⌊n/2⌋\begin{gathered} \,\log_2\, 2^{\lfloor \log_2\, n \rfloor - 1} \\ \le \log_2\, \lfloor n / 2 \rfloor \end{gathered}

      why it is allowed: A bigger number has a bigger log. For 11, the log of 4 is at most the log of 5.

    15. the move: the log of a power of 2 is the power

      the line:
      ⌊log⁡2 n⌋−1≤log⁡2 ⌊n/2⌋\begin{gathered} \lfloor \log_2\, n \rfloor - 1 \\ \le \log_2\, \lfloor n / 2 \rfloor \end{gathered}

      why it is allowed: For 11, the log of 4 is 2, at most about 2.32.

    16. the move: take the log of line 13

      the line:
       log⁡2 ⌊n/2⌋<log⁡2 2⌊log⁡2 n⌋\begin{gathered} \,\log_2\, \lfloor n / 2 \rfloor \\ < \log_2\, 2^{\lfloor \log_2\, n \rfloor} \end{gathered}

      why it is allowed: As in line 14. For 11, the log of 5 is below the log of 8.

    17. the move: the log of a power of 2 is the power

      the line:
       log⁡2 ⌊n/2⌋<⌊log⁡2 n⌋\begin{gathered} \,\log_2\, \lfloor n / 2 \rfloor \\ < \lfloor \log_2\, n \rfloor \end{gathered}

      why it is allowed: For 11, about 2.32 is below 3.

    18. the move: round the log down

      the line:
      ⌊log⁡2 ⌊n/2⌋⌋=⌊log⁡2 n⌋−1\begin{gathered} \lfloor \log_2\, \lfloor n / 2 \rfloor \rfloor \\ = \lfloor \log_2\, n \rfloor - 1 \end{gathered}
      the numberat leastbelowits log rounded down
      118163
      5482
      2241
      1120
      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 KK and below KK, so it rounds down to one less. For 11, about 2.32 rounds down to 2.

    19. the move: use line 18 again and again

      the line:
      after K halvings,rounding down,the log roundeddown is 0\begin{gathered} \text{after } K \text{ halvings,} \\ \text{rounding down,} \\ \text{the log rounded} \\ \text{down is 0} \end{gathered}

      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 KK. For 11, 3, 2, 1, 0.

    20. the move: a log rounded down to 0 belongs to 1

      the line:
      K halvings,rounding down,bring n to 1\begin{gathered} K \text{ halvings,} \\ \text{rounding down,} \\ \text{bring } n \text{ to 1} \end{gathered}

      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.

    the whole page: halving and logarithms

    why it is allowed: As in line 4, whole blocks end at 1 over 2 to the powers 1 to ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor, that many blocks. For 12, 3.

  14. the move: rounding takes off less than 1

    the line:
    H≥1+log⁡2 n−12H \ge 1 + \frac{\log_2\, n - 1}{2}

    why it is allowed: Rounding down takes off less than 1, so putting log⁡2 n\log_2\, n less 1 for ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor can only lower the right side. For 12, 2.5 becomes about 2.29.

  15. the move: halve each part

    the line:
    H≥1+log⁡2 n2−12H \ge 1 + \frac{\log_2\, n}{2} - \frac{1}{2}

    why it is allowed: Half of (log⁡2 n\log_2\, n less 1) is half of log⁡2 n\log_2\, n, less one half.

  16. the move: change the order

    the line:
    H≥1−12+log⁡2 n2H \ge 1 - \frac{1}{2} + \frac{\log_2\, n}{2}

    why it is allowed: Adding and taking away go in any order.

  17. the move: 1 less one half

    the line:
    H≥12+log⁡2 n2H \ge \frac{1}{2} + \frac{\log_2\, n}{2}

    why it is allowed: 1 less one half is one half.

  18. the move: drop the one half

    the line:
    H≥log⁡2 n2H \ge \frac{\log_2\, n}{2}

    why it is allowed: Taking away one half makes the right side smaller. For 12, about 1.79.

  19. the move: a floor

    the line:
    H∈Ω(log⁡2 n)H \in \Omega(\log_2\, n)
    the sumterms024681005101520the sumterms024681005101520
    • the ceiling, 2 log⁡2 n2 \,\log_2\, n
    • the harmonic sum
    • the floor, log⁡2 n2\frac{\log_2\, n}{2}
    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, HH is at least one half times log⁡2 n\log_2\, n for every nn from 2. H∈Ω(log⁡2 n)H \in \Omega(\log_2\, n), read HH is in big omega of log⁡2 n\log_2\, n, says a fixed multiple of log⁡2 n\log_2\, n, above zero, is a floor of HH.

  20. the move: ceiling and floor together

    the line:
    H∈Θ(log⁡2 n)H \in \Theta(\log_2\, n)

    why it is allowed: H∈Θ(log⁡2 n)H \in \Theta(\log_2\, n), read HH is in big theta of log⁡2 n\log_2\, n, says both: by lines 9 and 19, for every nn from 2, HH lies between one half and 2 times log⁡2 n\log_2\, n. It grows like log⁡2 n\log_2\, n.

try it with a small nn: at n=12n = 12, line 1 gives HH, about 3.1

a worked example

Take 1 to 4.

  1. Pick one, each as likely: 2.5 on average, by question 1.
  2. Shuffle them: 3 of the 6 pairs out of order on average, by question 2.
  3. Walk along a shuffle: 1 + 1 over 2 + 1 over 3 + 1 over 4, about 2.08 new bests, by question 3.
  4. 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

  1. 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.

  2. 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.

  3. 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.