[刷题] 统计首位数

练习1:统计从1到100的阶乘中,1到9做首位的次数

Python

import matplotlib.pyplot as plt
def first_digital(x):
    while x >= 10:
        x //= 10
    return x
if __name__ == '__main__':
    n = 1
    frequency = [0]*9
    for i in range(1,100):
        n *= i
        m = first_digital(n)-1
        frequency[m] += 1

print(frequency)
plt.plot(frequency,'r-',linewidth=2)
plt.grid(True)
plt.show()

本福特定律(第一数字定律):以1为首位数字出现的概率约30%,是直观想象1/9的三倍

猜你喜欢

转载自www.cnblogs.com/cxc1357/p/10367548.html