fork download
  1. import random
  2.  
  3. # Tamanhos das amostras para simulação
  4. m = [10, 50, 100, 250, 500, 1000, 1500, 5000, 10000, 15000, 50000, 100000]
  5.  
  6. simula_pi = dict()
  7. for n in m:
  8. ind = [] # True se o ponto está na círculo, False caso contrário
  9. for _ in range(n):
  10. x = random.uniform(-1, 1) # Eixo x da coordenada
  11. y = random.uniform(-1, 1) # Eixo y da coordenada
  12. ind.append((x**2 + y**2) <= 1) # True ou False
  13.  
  14. simula_pi[n] = ind
  15.  
  16. print("Tamanho\t\tE[I]\t\tPi")
  17. print("-------\t\t-------\t\t-------")
  18. for key in m:
  19. e_ind = sum(simula_pi[key])/len(simula_pi[key]) # E[I]
  20. pi = 4 * e_ind # Valor aproximado de Pi para a amostra
  21. print(str(key).ljust(12) + str(round(e_ind, 5)).ljust(12) + str(round(pi, 5)))
Success #stdin #stdout 0.24s 11252KB
stdin
Standard input is empty
stdout
Tamanho		E[I]		Pi
-------		-------		-------
10          0.8         3.2
50          0.74        2.96
100         0.78        3.12
250         0.808       3.232
500         0.772       3.088
1000        0.795       3.18
1500        0.792       3.168
5000        0.796       3.184
10000       0.7741      3.0964
15000       0.78827     3.15307
50000       0.78664     3.14656
100000      0.7851      3.1404