>>> import math >>> from collections import Counter >>> >>> def entropy(s): ... p, lns = Counter(s), float(len(s)) ... return -sum( count/lns * math.log(count/lns, 2) for count in p.values()) ... >>> >>> def fibword(nmax=37): ... fwords = ['1', '0'] ... print('%-3s %10s %-10s %s' % tuple('N Length Entropy Fibword'.split())) ... def pr(n, fwords): ... while len(fwords) < n: ... fwords += [''.join(fwords[-2:][::-1])] ... v = fwords[n-1] ... print('%3i %10i %10.7g %s' % (n, len(v), entropy(v), v if len(v) < 20 else '')) ... for n in range(1, nmax+1): pr(n, fwords) ... >>> fibword() N Length Entropy Fibword 1 1 -0 1 2 1 -0 0 3 2 1 01 4 3 0.9182958 010 5 5 0.9709506 01001 6 8 0.954434 01001010 7 13 0.9612366 0100101001001 8 21 0.9587119 9 34 0.9596869 10 55 0.959316 11 89 0.9594579 12 144 0.9594038 13 233 0.9594244 14 377 0.9594165 15 610 0.9594196 16 987 0.9594184 17 1597 0.9594188 18 2584 0.9594187 19 4181 0.9594187 20 6765 0.9594187 21 10946 0.9594187 22 17711 0.9594187 23 28657 0.9594187 24 46368 0.9594187 25 75025 0.9594187 26 121393 0.9594187 27 196418 0.9594187 28 317811 0.9594187 29 514229 0.9594187 30 832040 0.9594187 31 1346269 0.9594187 32 2178309 0.9594187 33 3524578 0.9594187 34 5702887 0.9594187 35 9227465 0.9594187 36 14930352 0.9594187 37 24157817 0.9594187 >>>