Python codes
Vertification of an assignment from Stochastic Processing. Using Brute Force and "itertools" library.
import itertools dim = 100 m = list(range(1, dim+1)) w = list(range(-1, -dim-1, -1)) num = 0 global zero_num zero_num = 0 m_all = list(itertools.permutations(m,dim)) w_all = list(itertools.permutations(w,dim)) num = len(m_all) * len(w_all) for m in m_all: for w in w_all: for i in range(dim): if m[i] + w[i] == 0: zero_num += 1 break print(zero_num) print(num) print((num-zero_num)/num)
As dim goes to infinity, the final ratio should be approaching to 1/e.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步