I cannot offer a closed form for the sum, but it is clear that the average cost peaks at powers of two with successive such peak values asymptotic up to 3.0 decay to the next power from two to the lower limit, which increases asymptotically to 2.0.
Each of the values (2 ^ n) - 1 strictly between 2 ^ n and 2 ^ (n + 1) contributes 1 to the total cost (causing the average to decay down) until the value at 2 ^ (n + 1) adds 2 ^ (n + 1). Therefore, the average contribution of the segment starting after 2 ^ n and ending at 2 ^ (n + 1) is ((2 ^ n) -1 + 2 ^ (n + 1)) / 2 ^ n or (3 * 2 ^ n - 1) / 2 ^ n, which approaches 3 with increasing n.
You can see both effects in the table below. Hope this helps.
i sum average ------- ------- ------- 1: 1 1.00000 2: 3 1.50000 3: 4 1.33333 4: 8 2.00000 ... 7: 11 1.57143 8: 19 2.37500 ... 15: 26 1.73333 16: 42 2.62500 ... 31: 57 1.83871 32: 89 2.78125 ... 63: 120 1.90476 64: 184 2.87500 ... 127: 247 1.94488 128: 375 2.92969 ... 255: 502 1.96863 256: 758 2.96094 ... 511: 1013 1.98239 512: 1525 2.97852 ... 1023: 2036 1.99022 1024: 3060 2.98828 ... 2047: 4083 1.99463 2048: 6131 2.99365 ... 4095: 8178 1.99707 4096: 12274 2.99658 ... 8191: 16369 1.99841 8192: 24561 2.99817 ... 16383: 32752 1.99915 16384: 49136 2.99902 ... 32767: 65519 1.99954 32768: 98287 2.99948 ... 65535: 131054 1.99976 65536: 196590 2.99973 ... 131071: 262125 1.99987 131072: 393197 2.99986 ... 262143: 524268 1.99993 262144: 786412 2.99992 ... 524287: 1048555 1.99996 524288: 1572843 2.99996 ... 1048575: 2097130 1.99998 1048576: 3145706 2.99998 ...
source share