# calculate_tau.piynb¶

### You should first run the notebook run_this_first.ipynb.¶

This script converts the first billion digits of pi into tau by the incredible algorithm of multiplying by two.

In order to avoid carry-the-one problems (which would occur at more than half the positions), the program does it in chunks, for every string of digits starting with the number 4, because no decimal-based number starting with a numeral of 0, 1, 2, 3 or 4 will gain a digit via carry-the-one when doubled. (It's true, think about it. Or just multiply 49999999999999999 by 2 in your calculator and see what happens.)

In [1]:
import time
import os

for i in range(10):

infilename = 'data/pi100m.dectxt.00' + str(i)
outfilename = 'data/tau100m.dectxt.00' + str(i)

if not os.path.isfile(outfilename):

start=time.time()

with open(infilename, 'r') as fin:
pi = fin.read()

overallcount = 100000000

for subset in range(9):
pisub = pi[subset*10000000:(subset+1)*10000000]

pos = 0
tau = ''
pisect=''
counter = 0

print "Processing " + infilename + ", remaining (million): ",

while pos < len(pisub):
if overallcount % 1000000 == 0:
print (overallcount / 1000000),
overallcount -= 1
if pisub[pos] == '4':
counter += 1 # I put in this counter to work on larger chunks, but I'm not convinced it makes a difference
if counter % 5 == 0:
numsect=int(pisect)
pisect='4'
numsect *= 2
tau+=str(numsect)
else:
pisect+=pisub[pos]
else:
pisect+=pisub[pos]
pos += 1
#for the remainder
numsect = int(pisect)
numsect *= 2
tau+=str(numsect)

with open(outfilename, 'a+') as fout:
fout.write(tau)

print 'Finished, %0.1f minutes elapsed' % ((time.time() - start)/60)

else:
print outfilename + 'already exists.'

print '\nScript finished.'

Processing data/pi100m.dectxt.000, remaining (million):  100 99 98 97 96 95 94 93 92 91 Processing data/pi100m.dectxt.000, remaining (million):  90 89 88 87 86 85 84 83 82 81 Processing data/pi100m.dectxt.000, remaining (million):  80 79 78 77 76 75 74 73 72 71 Processing data/pi100m.dectxt.000, remaining (million):  70 69 68 67 66 65 64 63 62 61 Processing data/pi100m.dectxt.000, remaining (million):  60 59 58 57 56 55 54 53 52 51 Processing data/pi100m.dectxt.000, remaining (million):  50 49 48 47 46 45 44 43 42 41 Processing data/pi100m.dectxt.000, remaining (million):  40 39 38 37 36 35 34 33 32 31 Processing data/pi100m.dectxt.000, remaining (million):  30 29 28 27 26 25 24 23 22 21 Processing data/pi100m.dectxt.000, remaining (million):  20 19 18 17 16 15 14 13 12 11 Finished, 2.3 minutes elapsed
Processing data/pi100m.dectxt.001, remaining (million):  100 99 98 97 96 95 94 93 92 91 Processing data/pi100m.dectxt.001, remaining (million):  90 89 88 87 86 85 84 83 82 81 Processing data/pi100m.dectxt.001, remaining (million):  80 79 78 77 76 75 74 73 72 71 Processing data/pi100m.dectxt.001, remaining (million):  70 69 68 67 66 65 64 63 62 61 Processing data/pi100m.dectxt.001, remaining (million):  60 59 58 57 56 55 54 53 52 51 Processing data/pi100m.dectxt.001, remaining (million):  50 49 48 47 46 45 44 43 42 41 Processing data/pi100m.dectxt.001, remaining (million):  40 39 38 37 36 35 34 33 32 31 Processing data/pi100m.dectxt.001, remaining (million):  30 29 28 27 26 25 24 23 22 21 Processing data/pi100m.dectxt.001, remaining (million):  20 19 18 17 16 15 14 13 12 11 Finished, 2.4 minutes elapsed
Processing data/pi100m.dectxt.002, remaining (million):  100 99 98 97 96 95 94 93 92 91 Processing data/pi100m.dectxt.002, remaining (million):  90 89 88 87 86 85 84 83 82 81 Processing data/pi100m.dectxt.002, remaining (million):  80 79 78 77 76 75 74 73 72 71 Processing data/pi100m.dectxt.002, remaining (million):  70 69 68 67 66 65 64 63 62 61 Processing data/pi100m.dectxt.002, remaining (million):  60 59 58 57 56 55 54 53 52 51 Processing data/pi100m.dectxt.002, remaining (million):  50 49 48 47 46 45 44 43 42 41 Processing data/pi100m.dectxt.002, remaining (million):  40 39 38 37 36 35 34 33 32 31 Processing data/pi100m.dectxt.002, remaining (million):  30 29 28 27 26 25 24 23 22 21 Processing data/pi100m.dectxt.002, remaining (million):  20 19 18 17 16 15 14 13 12 11 Finished, 2.4 minutes elapsed
Processing data/pi100m.dectxt.003, remaining (million):  100 99 98 97 96 95 94 93 92 91 Processing data/pi100m.dectxt.003, remaining (million):  90 89 88 87 86 85 84 83 82 81 Processing data/pi100m.dectxt.003, remaining (million):  80 79 78 77 76 75 74 73 72 71 Processing data/pi100m.dectxt.003, remaining (million):  70 69 68 67 66 65 64 63 62 61 Processing data/pi100m.dectxt.003, remaining (million):  60 59 58 57 56 55 54 53 52 51 Processing data/pi100m.dectxt.003, remaining (million):  50 49 48 47 46 45 44 43 42 41 Processing data/pi100m.dectxt.003, remaining (million):  40 39 38 37 36 35 34 33 32 31 Processing data/pi100m.dectxt.003, remaining (million):  30 29 28 27 26 25 24 23 22 21 Processing data/pi100m.dectxt.003, remaining (million):  20 19 18 17 16 15 14 13 12 11 Finished, 2.4 minutes elapsed
Processing data/pi100m.dectxt.004, remaining (million):  100 99 98 97 96 95 94 93 92 91 Processing data/pi100m.dectxt.004, remaining (million):  90 89 88 87 86 85 84 83 82 81 Processing data/pi100m.dectxt.004, remaining (million):  80 79 78 77 76 75 74 73 72 71 Processing data/pi100m.dectxt.004, remaining (million):  70 69 68 67 66 65 64 63 62 61 Processing data/pi100m.dectxt.004, remaining (million):  60 59 58 57 56 55 54 53 52 51 Processing data/pi100m.dectxt.004, remaining (million):  50 49 48 47 46 45 44 43 42 41 Processing data/pi100m.dectxt.004, remaining (million):  40 39 38 37 36 35 34 33 32 31 Processing data/pi100m.dectxt.004, remaining (million):  30 29 28 27 26 25 24 23 22 21 Processing data/pi100m.dectxt.004, remaining (million):  20 19 18 17 16 15 14 13 12 11 Finished, 2.3 minutes elapsed
Processing data/pi100m.dectxt.005, remaining (million):  100 99 98 97 96 95 94 93 92 91 Processing data/pi100m.dectxt.005, remaining (million):  90 89 88 87 86 85 84 83 82 81 Processing data/pi100m.dectxt.005, remaining (million):  80 79 78 77 76 75 74 73 72 71 Processing data/pi100m.dectxt.005, remaining (million):  70 69 68 67 66 65 64 63 62 61 Processing data/pi100m.dectxt.005, remaining (million):  60 59 58 57 56 55 54 53 52 51 Processing data/pi100m.dectxt.005, remaining (million):  50 49 48 47 46 45 44 43 42 41 Processing data/pi100m.dectxt.005, remaining (million):  40 39 38 37 36 35 34 33 32 31 Processing data/pi100m.dectxt.005, remaining (million):  30 29 28 27 26 25 24 23 22 21 Processing data/pi100m.dectxt.005, remaining (million):  20 19 18 17 16 15 14 13 12 11 Finished, 2.3 minutes elapsed
Processing data/pi100m.dectxt.006, remaining (million):  100 99 98 97 96 95 94 93 92 91 Processing data/pi100m.dectxt.006, remaining (million):  90 89 88 87 86 85 84 83 82 81 Processing data/pi100m.dectxt.006, remaining (million):  80 79 78 77 76 75 74 73 72 71 Processing data/pi100m.dectxt.006, remaining (million):  70 69 68 67 66 65 64 63 62 61 Processing data/pi100m.dectxt.006, remaining (million):  60 59 58 57 56 55 54 53 52 51 Processing data/pi100m.dectxt.006, remaining (million):  50 49 48 47 46 45 44 43 42 41 Processing data/pi100m.dectxt.006, remaining (million):  40 39 38 37 36 35 34 33 32 31 Processing data/pi100m.dectxt.006, remaining (million):  30 29 28 27 26 25 24 23 22 21 Processing data/pi100m.dectxt.006, remaining (million):  20 19 18 17 16 15 14 13 12 11 Finished, 2.3 minutes elapsed
Processing data/pi100m.dectxt.007, remaining (million):  100 99 98 97 96 95 94 93 92 91 Processing data/pi100m.dectxt.007, remaining (million):  90 89 88 87 86 85 84 83 82 81 Processing data/pi100m.dectxt.007, remaining (million):  80 79 78 77 76 75 74 73 72 71 Processing data/pi100m.dectxt.007, remaining (million):  70 69 68 67 66 65 64 63 62 61 Processing data/pi100m.dectxt.007, remaining (million):  60 59 58 57 56 55 54 53 52 51 Processing data/pi100m.dectxt.007, remaining (million):  50 49 48 47 46 45 44 43 42 41 Processing data/pi100m.dectxt.007, remaining (million):  40 39 38 37 36 35 34 33 32 31 Processing data/pi100m.dectxt.007, remaining (million):  30 29 28 27 26 25 24 23 22 21 Processing data/pi100m.dectxt.007, remaining (million):  20 19 18 17 16 15 14 13 12 11 Finished, 2.2 minutes elapsed
Processing data/pi100m.dectxt.008, remaining (million):  100 99 98 97 96 95 94 93 92 91 Processing data/pi100m.dectxt.008, remaining (million):  90 89 88 87 86 85 84 83 82 81 Processing data/pi100m.dectxt.008, remaining (million):  80 79 78 77 76 75 74 73 72 71 Processing data/pi100m.dectxt.008, remaining (million):  70 69 68 67 66 65 64 63 62 61 Processing data/pi100m.dectxt.008, remaining (million):  60 59 58 57 56 55 54 53 52 51 Processing data/pi100m.dectxt.008, remaining (million):  50 49 48 47 46 45 44 43 42 41 Processing data/pi100m.dectxt.008, remaining (million):  40 39 38 37 36 35 34 33 32 31 Processing data/pi100m.dectxt.008, remaining (million):  30 29 28 27 26 25 24 23 22 21 Processing data/pi100m.dectxt.008, remaining (million):  20 19 18 17 16 15 14 13 12 11 Finished, 2.2 minutes elapsed
Processing data/pi100m.dectxt.009, remaining (million):  100 99 98 97 96 95 94 93 92 91 Processing data/pi100m.dectxt.009, remaining (million):  90 89 88 87 86 85 84 83 82 81 Processing data/pi100m.dectxt.009, remaining (million):  80 79 78 77 76 75 74 73 72 71 Processing data/pi100m.dectxt.009, remaining (million):  70 69 68 67 66 65 64 63 62 61 Processing data/pi100m.dectxt.009, remaining (million):  60 59 58 57 56 55 54 53 52 51 Processing data/pi100m.dectxt.009, remaining (million):  50 49 48 47 46 45 44 43 42 41 Processing data/pi100m.dectxt.009, remaining (million):  40 39 38 37 36 35 34 33 32 31 Processing data/pi100m.dectxt.009, remaining (million):  30 29 28 27 26 25 24 23 22 21 Processing data/pi100m.dectxt.009, remaining (million):  20 19 18 17 16 15 14 13 12 11 Finished, 2.2 minutes elapsed

Script finished.


#### Verification: compare first 100000 digits to those listed on tauday.com¶

In [3]:
tauday_file = open('data/tau100K-tauday.com.txt', 'r')
tauday_string = tauday_file.read()
tauday_file.close()
this_file = open('data/tau100m.dectxt.000', 'r')
this_string = this_file.read()
this_file.close()

print len(tauday_string)
print tauday_string[:10]
print this_string[:10]

if tauday_string[:99999] == this_string[:99999]:
print "Verification successful. Yay."

99999
2831853071
2831853071
Verification successful. Yay.

##### Create 1000-character files for overlaps during tests¶
In [8]:
filelist = ['data/tau100m.dectxt.001', 'data/tau100m.dectxt.002', 'data/tau100m.dectxt.003',
'data/tau100m.dectxt.004', 'data/tau100m.dectxt.005', 'data/tau100m.dectxt.006',
'data/tau100m.dectxt.007', 'data/tau100m.dectxt.008', 'data/tau100m.dectxt.009']

for filename in filelist:
current = open(filename, 'r')
string1K = current.read()[:1000]
current.close()
newfile = open(filename+'.1K', 'w+')
newfile.write(string1K)
newfile.close()


##### Verify some of the regex-style search results¶
In [4]:
# verify that pi has a fibonacci sequence of 11235813 at position 48300973
this_file = open('data/pi100m.dectxt.000', 'r')
this_string = this_file.read()
this_file.close()
print this_string[48300973:48300993]

11235813455381270624

In [5]:
# verify that tau has a fibonacci sequence of 1123581321 at position 809073288
this_file = open('data/tau100m.dectxt.008', 'r')
this_string = this_file.read()
this_file.close()
print this_string[9073288:9073300]

112358132150

In [6]:
# verify that tau recapitulates pi at position 52,567,169
this_file = open('data/tau100m.dectxt.000', 'r')
this_string = this_file.read()
this_file.close()
print this_string[52567169:52567189]

31415926531269297186

In [7]:
# verify that tau recapitulates tau at position 405747241
this_file = open('data/tau100m.dectxt.004', 'r')
this_string = this_file.read()
this_file.close()
print this_string[5747241:5747261]

62831853035460504279

In [ ]: