Created
April 20, 2017 21:50
-
-
Save rkiyanchuk/53acb78d9d5e316b278b6ab6a1423a5d to your computer and use it in GitHub Desktop.
Benford's law in Fibonacci sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Verify that Fibonacci sequence follows Benford's law.""" | |
from pprint import pprint | |
def fibonacci(n): | |
a, b = 0, 1 | |
for i in range(n): | |
a, b = b, a + b | |
return a | |
def lead_num_freq(nums): | |
freqs = dict(zip(range(1, 10), [0]*9)) | |
for i in nums: | |
lead = str(i)[0] | |
freqs[int(lead)] += 1 | |
for digit in freqs: | |
freqs[digit] = freqs[digit] / len(nums) * 100 | |
return freqs | |
def main(): | |
fibs = [fibonacci(i) for i in range(1, 10000)] | |
pprint(lead_num_freq(fibs), width=1) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Benford's law