def mean_distance(blocksize, numblocks):
blocks = [int.from_bytes(data[i:i+blocksize], 'big')
for i in range(0, numblocks * blocksize, blocksize)]
score = sum(popcount(u ^ v)
for u, v in combinations(blocks, 2))
numpairs = numblocks * (numblocks - 1) // 2
return score / numpairs
counts = Counter()
for numblocks in range(2, 16):
a = []
for keysize in range(2, 40):
d = mean_distance(keysize, numblocks) / keysize
a.append((d, keysize))
a.sort()