Monte Carlo Methods

E is defined as follows:

 



There are two ways to solve it:
First, formula,

Take a large number substitution.

python code is as follows:

1 x = 1024*1024
2 e = pow(1+1/x,x)
3 print("{}".format(e))

 


Second, the Monte Carlo method (the title itself is not difficult, is the focus of this mode of thinking)

 

. 1  Import Random AS RD
 2  
. 3 scatterNum = 1024 * 1024 # Total point spread 
. 4 COUNT = 0
 . 5  
. 6  for I in Range (scatterNum):
 . 7      X = rd.uniform (1,2 )
 . 8      Y = rd.uniform (0 ,. 1 )
 . 9      IF X * Y <=. 1 :
 10          COUNT = +. 1
 . 11  E = POW (2, scatterNum / COUNT)
 12 is  Print ( " NATURAL constant E: {} " .format (E))

 

Guess you like

Origin www.cnblogs.com/zach0812/p/11271928.html