-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d087afd
commit 3addde7
Showing
2 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
maxq=maxn=maxr=maxk=maxl=maxt=0 | ||
|
||
def calcPi(limit): # Generator function | ||
""" | ||
Prints out the digits of PI | ||
until it reaches the given limit | ||
""" | ||
|
||
q, r, t, k, n, l = 1, 0, 1, 1, 3, 3 | ||
|
||
|
||
decimal = limit | ||
counter = 0 | ||
|
||
while counter != decimal + 1: | ||
if 4 * q + r - t < n * t: | ||
# yield digit | ||
yield n | ||
# insert period after first digit | ||
if counter == 0: | ||
yield '.' | ||
# end | ||
if decimal == counter: | ||
print('') | ||
break | ||
counter += 1 | ||
nr = 10 * (r - n * t) | ||
n = ((10 * (3 * q + r)) // t) - 10 * n | ||
q *= 10 | ||
r = nr | ||
maxn=max(n,maxn) | ||
maxq=max(q,maxq) | ||
maxr=max(r,maxr) | ||
|
||
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 | ||
maxn=max(n,maxn) | ||
maxq=max(q,maxq) | ||
maxr=max(r,maxr) | ||
maxt=max(t,maxt) | ||
maxl=max(l,maxl) | ||
maxk=max(k,maxk) | ||
|
||
|
||
|
||
|
||
# Calls CalcPi with the given limit | ||
pi_digits = calcPi(int(input("Enter the number of decimals to calculate to: "))) | ||
|
||
i = 0 | ||
|
||
# Prints the output of calcPi generator function | ||
# Inserts a newline after every 40th number | ||
for d in pi_digits: | ||
print(d, end='') | ||
i += 1 | ||
if i == 40: | ||
print("") | ||
i = 0 | ||
|
||
|
||
print("n:",maxn,"q:",maxq) |