Okay, after long delay, the analytic solution:
First, generate the binary expansion of the repeating binary representation of 1/N (see, Achernar, you were right
)
let k be the number of digits which are repeated and i be a position of a particular 1 within the repeating unit.
Each one in the repeating unit represents an infinite geometric series. The expected number of outcomes is equal to the probability of each one (N times 2^-number of flips) times the number of flips.
The average number of flips will be N times the infinite summation for each of the ones in the binary expansion:
N*(sum over all i where digit is 1) of:
i*x^i + (i+k)x^(i+k) + (i+2k)x^(i+2k) …
Now, to solve this infinite series, start with the most simple geometric one:
1 + x + x^2 + x^3 … = 1/(1-x)
Note that x will be 1/2 throughout. Substitute x^k for x.
1 + x^k + x^2k + … = 1/(1-x^k)
Multiply both sides by x^i
x^i + x^(i+k) + x^(i+2k) … = x^i/(1/x^k)
Now the trick- differentiate both sides.
ix^(i-1) + (i+k)x^(i+k-1) + (i+2k)x^(i+2k-1) … = ix^(i-1)/(1/x^k)+x^ix^(k-1)*(1/x^k)^2
Multiply both sides by x and simplify:
i*x^i + (i+k)x^(i+k) + (i+2k)x^(i+2k) … = {ix^i+(k-i)x^(k+i)}/(1-x^k)^2
The average number of flips required is therefore equal to N times the sum of {ix^i+(k-i)x^(k+i)}/(1-x^k)^2 over all i’s in the binary expansion of 1/N.
I’ve coded this, and to works… sometimes. ( I think I’m exceeding limits on integers.) Anyhow, here are some exact results. The format is N, numerator, denominator.
3 8 3
5 18 5
7 24 7
9 14 3
11 160 33
13 306 65
15 64 15
17 98 17
19 2936 513
21 38 7
23 11672 2047
25 1138 205
27 320 57
29 89074 16385
31 160 31
33 226 33
35 27592 4095
37 4669510380151843082 -137438953471
39 600 91
41 6714 1025
43 896 129
45 9274 1365
47 10107193409760 1497206965967
49 1985714 299593
51 328 51
53 -2957180271666859382 -9007199254740991
55 91048 13981
57 1118 171
59 7387341914293869920 -576460752303423487
61 8924378185174938962 -2305843009213693951
63 128 21
65 514 65
67 -5149539208175850664 1
69 32740246 4194303
71 79853147459949752 -1963413621
73 554 73
75 1621984 209715
77 8215881550 1073741823
79 23778824631758272 -35468117025
81 7898475704543561282 -36028797018963967
83 7301213462461240952 1
85 626 85
87 2117196184 268435455
89 15942 2047
91 704 91
93 7822 1023
95 -1452444394240966176 -137438953471
97 -8550538500261448030 -562949953421311
99 82792 10923
This shows that the formula is sound, even if my implementation of it is flawed for some numbers (e.g. 37)
Tim