-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenfords_law.py
41 lines (34 loc) · 904 Bytes
/
benfords_law.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from matplotlib import pyplot as plt
import math
dp = {}
r = 100000
digits = list(range(1, 10))
freq = [0]*len(digits)
for i in range(1, r + 1):
n = i
current = [0]*len(digits)
while n > 1:
if n in dp:
for j in range(9):
freq[j] += dp[n][j]
current[j] += dp[n][j]
break
d = int(math.log10(n))
fd = int(n / pow(10, d))
freq[fd-1] += 1
current[fd-1] += 1
if n & 1:
n = (3 * n + 1) // 2
else:
n >>= 1
freq[0] += 1
dp[i] = current
values = []
for a, f in zip(digits, freq):
values.extend(a for _ in range(f))
fig, axs = plt.subplots(1, 1, figsize=(10, 6))
axs.hist(x=values, range=[1, 9], rwidth=0.9, align="mid", orientation="vertical")
plt.xlabel("First digit in steps count")
plt.ylabel("Frequency")
plt.title("Benford's Law")
plt.show()