Write Python program that calculates digits of pi number.
- User
Write Python program that calculates digits of pi number.
- Assistant
Sure! Here's a Python program that calculates the digits of pi using the Bailey–Borwein–Plouffe formula:
pythonimport decimal def calculate_pi(digits): decimal.getcontext().prec = digits + 2 pi = decimal.Decimal(0) for k in range(digits): pi += (decimal.Decimal(1)/(16**k))*((decimal.Decimal(4)/(8*k+1)) - (decimal.Decimal(2)/(8*k+4)) - (decimal.Decimal(1)/(8*k+5)) - (decimal.Decimal(1)/(8*k+6))) return pi # Get user input num_of_digits = int(input("Enter the number of decimal places to calculate for pi: ")) # Calculate pi pi = calculate_pi(num_of_digits) # Print pi print(f"Pi: {pi}")