-
-
Save angeloped/b62c85884dc7ca0d5a26bc7c90af4981 to your computer and use it in GitHub Desktop.
pi is a Python script that computes each digit of the value of pi. As long as this script runs it continues to generate digits. As a matter of full disclosure, I did not write this. I am posting it here as a means of preserving the algorithm and making it available to others.
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
import sys | |
''' | |
Added some modificationss | |
1. saving line for every 1024 digits. | |
2. notify if it scored million. | |
''' | |
def calcPi(): | |
q, r, t, k, n, l = 1, 0, 1, 1, 3, 3 | |
while True: | |
if 4*q+r-t < n*t: | |
yield n | |
nr = 10*(r-n*t) | |
n = ((10*(3*q+r))//t)-10*n | |
q *= 10 | |
r = nr | |
else: | |
nr = (2*q+r)*l | |
nn = (q*(7*k)+2+(r*l))//(t*l) | |
q *= k | |
t *= l | |
l += 2 | |
k += 1 | |
n = nn | |
r = nr | |
pi_digits = calcPi() | |
i = 0 | |
data="" | |
loop = 0 | |
prnt = 1000000 | |
for d in pi_digits: | |
#print(str(d)) | |
i += 1 | |
data += str(d) | |
if i == 1024: | |
with open("PI","a+") as pif: | |
pif.write(data + "\n") | |
i = 0 | |
data = "" | |
loop += 1 | |
if loop >= prnt: | |
print("I made " + str(loop) + " decimals (M)") | |
prnt += 1000000 | |
if loop >= 5400000000: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment