An academic question. This function computes Benford's Law for integers up to maxvalue, and prints a summary table. I've tried a nested for-loop method, a dict method, and this collections method. The latter (code below) seems the fastest (timeit result: 1.4852424694 sec), but is there a faster and memory-efficient method for cycling through so many possibilities?
from __future__ import print_function
def BenfordsLaw4(maxvalue = 10**6):
from collections import Counter
sqList = (str((i+1)**2)[0] for i in range(maxvalue))
BenfordList = Counter(sqList)
print("Benford's Law for numbers between 1 and", maxvalue, "\nDigits,\t\t\t", "Count,\t\t\t", "Percentage")
for i,j in sorted(BenfordList.iteritems()):
print(',\t\t\t\t'.join([str(i), str(j), str(j*100./maxvalue)+' %']))