🐍 Enter a number and have the program generate π (pi) up to that many decimal places. Keep a limit to how far the program will go.
7 Different Solutions
Solution 1
from decimal import *
def pipric(n):
getcontext().prec = n+1
return Decimal(22)/Decimal(7)
if __name__ == '__main__':
correct_input = False
while not correct_input:
print('Precision must be between 1 and 51')
precision = int(input('Number of decimal places: '))
if 51 >= precision > 0:
correct_input = True
print(pipric(precision))
Solution 2
def pipric(n):getcontext().prec = n+1return '%.*f' % (n, 22/7)
if __name__ == '__main__':correct_input = Falsewhile not correct_input:print('Precision must be between 1 and 51')precision = int(input('Number of decimal places: '))if 51 >= precision > 0:correct_input = Trueprint(pipric(precision))
Solution 3: More precise
#!/usr/bin/env python3
# https://github.com/MrBlaise/learnpython/blob/master/Numbers/pi.py
# Find PI to the Nth Digit
# Have the user enter a number 'n'
# and print out PI to the 'n'th digit
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
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
def main(): # Wrapper function
# 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
if __name__ == '__main__':
main()
Solution 4
#https://github.com/rlingineni/PythonPractice/blob/master/piCalc/pi.py
import math
def CalculatePi(roundVal):
somepi = round(math.pi,roundVal);
pi = str(somepi)
someList = list(pi)
return somepi;
roundTo = input('Enter the number of digits you want after the decimal for Pi: ')
try:
roundint = int(roundTo);
print(CalculatePi(roundint));
except:
print("You did not enter an integer");
Comments
Post a Comment