codepad | about

 Link:

Python, pasted on Jan 29:
 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ``` ```""" pi.py Calculates an approximation of pi given the multiples 0pi - 1000pi using the Pigeonhole Principle. Outputs the first approximation found. Author: Louis Li """ import math def main(): # Store the decimals we've found in a dictionary with its multiple of pi d = dict() for n in xrange(0, 1001): n_pi = n * math.pi first_three_dec = int(math.floor((n_pi * 1000) % 1000)) if not first_three_dec in d: d[first_three_dec] = n else: # Calculate an approximation, having found two numbers # with the same first three decimal places m = d[first_three_dec] m_pi = m * math.pi print int(round(n_pi - m_pi)) print "----- = ", ((n_pi - m_pi) / (n - m)), \ "(m = %d, n = %d)" % (m, n) print (n - m) print "\nActual value, pi: ", math.pi break if __name__ == '__main__': main() ```

Output:
 ```1 2 3 4 5 ``` ```355 ----- = 3.14159265359 (m = 1, n = 114) 113 Actual value, pi: 3.14159265359 ```

Comments: