Skip to content

Instantly share code, notes, and snippets.

@EhsanKia
Created March 14, 2017 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EhsanKia/2d5f3e956484534858b0912dc795f5a9 to your computer and use it in GitHub Desktop.
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
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