Skip to content

Instantly share code, notes, and snippets.

@tclarke
Created September 6, 2013 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tclarke/6458240 to your computer and use it in GitHub Desktop.
Save tclarke/6458240 to your computer and use it in GitHub Desktop.
A fun little example of Benford's Law
import numpy
def fib(a,b):
"Generator for the fibonacci sequence starting at position a,b"
while True:
yield b
a,b=b,a+b
def ld(v):
"Determine the left-most digit in v"
return (v%10) if (v<10) else ld(v//10)
def calc_hist(n,a=0,b=1):
"Calculate the histogram of leading digits for n numbers in the fibonacci sequence starting at position a,b"
hist=numpy.zeros(9, dtype="float")
f=fib(a,b)
for i in xrange(n):
hist[ld(f.next())-1] += 1
return hist/hist.sum()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment