Benford's Law Example

In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [3]:
# first digit of a number

def firstDigit(n):
    string = str(n)
    return int(string[0])
In [9]:
# exponentiate and track first digit at each step

def benford(num,iter):
    arrNum = [num]
    for i in range(iter):
        arrNum.append(arrNum[-1]*num)
    return [firstDigit(n) for n in arrNum]
In [13]:
# histogram for 2.04

plt.hist(benford(2.04,100),bins=9)
plt.show()
In [ ]: