Jupyter——九点分布概率图
计算n=1,2,3,…,N的n!首个数字出现1,2,3,4,5,6,7,8,9的概率
def Getfirst(x):
x = list(str(x))[0]
return int(x)
def P_first(N):
x = 1
p = [0]*9
for i in range(1,N+1):
x = x*i
p[Getfirst(x)-1]+=1
return [round(k/N,3) for k in p]
N=100
p = P_first(N)
print(p)
[0.3, 0.18, 0.13, 0.07, 0.07, 0.07, 0.03, 0.1, 0.05]
九点分布可视化——散点图
import matplotlib.pyplot as plt
%matplotlib inline
from matplotlib.font_manager import FontProperties
font_set = FontProperties(fname='C:\Windows\Fonts\msyh.ttf', size=12)
plt.plot(p, 'r',linewidth = 2)
plt.plot(p, 'go',markersize = 8)
plt.xticks(list(range(0,9)),list(range(1,10)))
plt.grid = True
plt.title('九点分布:N= '+ str(N), fontproperties=font_set)
for a,b in zip(list(range(0,9)),p):
plt.text(a+0.25, b, b,fontsize=10)
plt.show()

