'Calculating Pi to the Nth digit

I'm trying to enter in a number and calculate pi to that digit input. I managed to be able to calculate Pi, however no matter what number I type it will still generate the same amount of Pi numbers.

I'm a bit confused at what point it's causing to do that

from math import factorial
from decimal import Decimal, getcontext
# Chudnovsky algorithm for figuring out pi
getcontext().prec=100

pi_input = input('How many digits of pi would you like?')
n = int(pi_input)

def calc(n):
    t= Decimal(0)
    pi = Decimal(0)
    deno= Decimal(0)

    for k in range(n):
        t = ((-1)**k)*(factorial(6*k))*(13591409+545140134*k)
        deno = factorial(3*k)*(factorial(k)**3)*(640320**(3*k))
        pi += Decimal(t)/Decimal(deno)
    pi = pi * Decimal(12) / Decimal(640320 ** Decimal(1.5))
    pi = 1/pi
    return pi

print calc(n)

Here is my output

How many digits of pi would you like? 5 

3.141592653589793238462643383279502884197169399375105820974944592307816346
94690247717268165239156011


Solution 1:[1]

Using the Chudnovsky algorithm, the calculation produces about 14.18 decimal digits per iteration: log10((640320^3)/(24*6*2*6)) ~= 14.18. This can be more clearly seen in the formula for ak / ak-1 as shown on this web page:

https://www.craig-wood.com/nick/articles/pi-chudnovsky

For n = 5, the result has about 70 digits of precision.

Solution 2:[2]

I just added round function in the return statement in your code and hope and it works for you as it for me.

from math import factorial
from decimal import Decimal, getcontext
# Chudnovsky algorithm for figuring out pi
getcontext().prec=1000

pi_input = input('How many digits of pi would you like?')
n = int(pi_input)

def cal(n):
    t= Decimal(0)
    pi = Decimal(0)
    deno= Decimal(0)

    for k in range(n):
        t = ((-1)**k)*(factorial(6*k))*(13591409+545140134*k)
        deno = factorial(3*k)*(factorial(k)**3)*(640320**(3*k))
        pi += Decimal(t)/Decimal(deno)
    pi = pi * Decimal(12) / Decimal(640320 ** Decimal(1.5))
    pi = 1/pi
    return round(pi,n)

print(cal(n))

Solution 3:[3]

you can use "%.nf"to format the output string where n is the number of digits you want to output. e.g.

import numpy as np
print "%.5f"%(np.pi)

Solution 4:[4]

from math import factorial
from decimal import Decimal, getcontext

n = int(input('How many digits of pi would you like?'))
# Chudnovsky algorithm for figuring out pi
getcontext().prec=n+1
def calc(n):
    t= Decimal(0)
    pi = Decimal(0)
    deno= Decimal(0)
    k=0

    #t = ((-1)**k)*(factorial(6*k))*(13591409+545140134*k)
    t=(1)*(factorial(1))*(13591409+545140134*k)
    deno = factorial(3*k)*(factorial(k)**3)*(640320**(3*k))
    pi += Decimal(t)/Decimal(deno)
    pi = pi * Decimal(12) / Decimal(640320 ** Decimal(1.5))
    pi = 1/pi
    return pi

print (calc(n))

Solution 5:[5]

this might be a simple code for understanding

from numpy import *

n = int(input('How many digits of pi after decimal would you like to print'))
print(pi)

#print (" value of pi at {:.4f} is" .format(pi))

print('{pi:0.{precision}f}'.format(pi=pi,precision=n))

Solution 6:[6]

this is how I would do it :-)

import math
digits = int(input("to how many digits to you want to round PI?"))

def roundpi(n):
    return round(pi,n)

roundpi(digits)

Solution 7:[7]

Please try this and let me know if it works for you

import numpy as np

def pi_nth(n):
    new_str = ''
    for i in range(n+2):
        new_str += str(np.pi)[i]
    return float(new_str)    

Solution 8:[8]

I answered the same question the short way using PI from the math module.

from math import pi
print()
nums = int(input("Enter the number of decimals: "))
print("Pi to the {}th number of decimals is %.{}f".format(nums, nums) % (pi))

Output

Enter the number of decimals: 5
Pi to the 5th number of decimals is 3.14159

This is in python 3.8 hope it helps.

Solution 9:[9]

#To print the Nth decimal place values of pi
from math import pi
#Receive the input value for number of decimal points of pi needed from user
i=input("Enter the number of decimal places needed in pi")
#Variable to hold the pi value upto nth decimal.Assign it to empty string initially
n_val=""
#Convert the pi value to string 
string_pi=str(pi) 

    x=0
    #loop through each literals and add the value to a string variable and then print it
    while x<=int(i)+1:
        n_val+=string_pi[x]
        x=x+1
    print(n_val)
    

Solution 10:[10]

from math import pi

num = int(input('Enter the number of decimals: '))

print(f"Pi upto {num}th number is {pi:{1}.{num+1}}")