Created
July 24, 2012 22:02
-
-
Save samposm/3172978 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/python | |
# ref. discussion in: https://news.ycombinator.com/item?id=4285531 | |
import random | |
import math | |
max1, max2, iterations = 999, 255, 1000 | |
pi_approx = 355/113. - math.pi | |
fracs1 = sum([[i/float(n) for i in range(n+1)] for n in range(1,max1+1)], []) | |
fracs2 = sum([[i/float(n) for i in range(n+1)] for n in range(1,max2+1)], []) | |
def test(fracs): | |
r = random.random() | |
return len(filter(lambda x: abs(x-r) < pi_approx, fracs)) > 0 | |
print [test(fracs1) for i in range(iterations)].count(True),"/", iterations | |
print [test(fracs2) for i in range(iterations)].count(True),"/", iterations |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment