Created
March 14, 2017 01:13
-
-
Save EhsanKia/2d5f3e956484534858b0912dc795f5a9 to your computer and use it in GitHub Desktop.
Generate Pi from checking if two random numbers are coprime. https://www.youtube.com/watch?v=RZBhSi_PwHU
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
from math import sqrt | |
from random import randint | |
try: | |
from math import gcd | |
except: | |
from fractions import gcd | |
ITERATIONS = 1000000 | |
MAX_INT = 1000000000 | |
total = 0 | |
for i in range(ITERATIONS): | |
a, b = randint(1, MAX_INT), randint(1, MAX_INT) | |
total += gcd(a, b) == 1 | |
print(sqrt(6.0 * ITERATIONS / total)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment