Created
January 17, 2022 12:26
-
-
Save Ian729/f7d8ed063d50901446fa167a721347ec to your computer and use it in GitHub Desktop.
Monte Carlo Pi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
# number of all points in 1/4 circle / number of all points = pi/4 | |
def pi(n): return sum( [random.random()**2 + random.random()**2 <= 1 for _ in range(n)] ) / n * 4 | |
# % python3 -i pi.py | |
print(pi(100)) | |
# 3.52 | |
print(pi(1000)) | |
# 3.104 | |
print(pi(10000)) | |
# 3.1448 | |
print(pi(100000)) | |
# 3.13572 | |
# print(pi(1000000)) | |
# 3.14102 | |
# print(pi(10000000)) | |
# 3.1422764 | |
# print(pi(100000000)) | |
# 3.14178852 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment