Created
May 19, 2015 18:36
-
-
Save apiarian/b082685279e0afd68895 to your computer and use it in GitHub Desktop.
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
#! /usr/bin/env python3 | |
# http://www.wolframalpha.com/input/?i=1000+digits+of+pi | |
pi_str = '3.1415926535897932384626'\ | |
'433832795028841971693993751058209'\ | |
'749445923078164062862089986280348'\ | |
'253421170679821480865132823066470'\ | |
'938446095505822317253594081284811'\ | |
'174502841027019385211055596446229'\ | |
'489549303819644288109756659334461'\ | |
'284756482337867831652712019091456'\ | |
'485669234603486104543266482133936'\ | |
'072602491412737245870066063155881'\ | |
'748815209209628292540917153643678'\ | |
'925903600113305305488204665213841'\ | |
'469519415116094330572703657595919'\ | |
'530921861173819326117931051185480'\ | |
'744623799627495673518857527248912'\ | |
'279381830119491298336733624406566'\ | |
'430860213949463952247371907021798'\ | |
'609437027705392171762931767523846'\ | |
'748184676694051320005681271452635'\ | |
'608277857713427577896091736371787'\ | |
'214684409012249534301465495853710'\ | |
'507922796892589235420199561121290'\ | |
'219608640344181598136297747713099'\ | |
'605187072113499999983729780499510'\ | |
'597317328160963185950244594553469'\ | |
'083026425223082533446850352619311'\ | |
'881710100031378387528865875332083'\ | |
'814206171776691473035982534904287'\ | |
'554687311595628638823537875937519'\ | |
'577818577805321712268066130019278'\ | |
'76611195909216420199' | |
def decimal_places(s): | |
return len(s) - s.find('.') - 1 | |
def shifted_integer(s): | |
return int(s.replace('.','')) | |
def rebuild_decimal_string(i,d): | |
s = str(i) | |
return s[0]+'.'+s[1:] | |
# diameter of the observable universe | |
# http://www.wolframalpha.com/input/?i=880000000000000000000000000m | |
diameter_str = '88'+''.join(['0' for x in range(25)]) # m | |
# diameter of the earth | |
# http://www.wolframalpha.com/input/?i=diameter+of+the+earth+in+m | |
#diameter_str = '12734889' | |
# just a test | |
#diameter_str = '1000' | |
diameter = int(diameter_str) | |
significant_digits = 2 | |
while significant_digits <= len(pi_str)-1: | |
truncated_pi_str = pi_str[0:1+significant_digits] | |
truncated_pi_decimal_places = decimal_places(truncated_pi_str) | |
truncated_pi_integer = shifted_integer(truncated_pi_str) | |
truncated_pi_integer_plus = truncated_pi_integer + 1 | |
truncated_pi_str_plus = rebuild_decimal_string(truncated_pi_integer_plus, truncated_pi_decimal_places) | |
circumference_str = str(truncated_pi_integer * diameter)[0:-truncated_pi_decimal_places] | |
circumference_str_plus = str(truncated_pi_integer_plus * diameter)[0:-truncated_pi_decimal_places] | |
difference = int(circumference_str_plus) - int(circumference_str) | |
print('{}m * {} = {}m'.format(diameter, truncated_pi_str, circumference_str)) | |
print('{}m * {} = {}m'.format(diameter, truncated_pi_str_plus, circumference_str_plus)) | |
print('{}m - {}m = {}m, ({}%)'.format(circumference_str_plus, circumference_str, difference,100*difference/int(circumference_str))) | |
print() | |
if difference == 0: | |
print('significant digits: {}'.format(significant_digits)) | |
print(truncated_pi_str) | |
break | |
significant_digits = significant_digits + 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment