from math import * from random import * from matplotlib.pyplot import * def rec(f=lambda x: sqrt(1-x**2),a=0,b=1,n=10000 ,gd=0): d=(b-a)/n xi=a+gd*d s = 0 for _i in range(n): s += f(xi) xi += d return s*d def disque(x,y): return x**2+y**2 <1 def montecarlo(n=1000): s=0 for _i in range(n): if disque(random(),random()) : s += 1 return s/n N=10 X=[2**i for i in range(N)] Ym=[montecarlo(n=X[i]) for i in range(N)] Yr=[rec(n=X[i]) for i in range(N)] plot(X,Ym) plot(X,Yr) show()