Calculating Pi to the Nth digit

13,607

Solution 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

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))
Share:
13,607

Related videos on Youtube

Blooze
Author by

Blooze

Updated on June 04, 2022

Comments

  • Blooze
    Blooze almost 2 years

    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
    
    • klutt
      klutt almost 7 years
      What makes you think it will produce n decimals? As far as I can see it will produce better results with a higher n but certainly not exactly n digits. And no, the result is different if you change n.
    • sascha
      sascha almost 7 years
      Without analyzing the code: what do you mean with your output? Show an example! It is possible, that the output is just a floating-point print and therefore the number of digits seen is not dependent on how many digits you computed? Be more verbose
    • klutt
      klutt almost 7 years
      To be clear, you will output the same amount of digits regardless of what n you pick, but the result will be more accurate with higher n.
    • Blooze
      Blooze almost 7 years
      Updated my output, I guess I am confused how I can make my function perform the calculation based on the input
  • il_raffa
    il_raffa almost 6 years
    Along with the code, you should also add a description of the solution you're proposing.
  • Luo Hongshuai
    Luo Hongshuai over 4 years
    not work for an N greater than 50 3.1415926535897931159979634685441851615905761718750000000000‌​0000000
  • PirateNinjas
    PirateNinjas about 4 years
    rather than iterating you could use a slice, given that you're not calculating pi, per se.
  • Ruli
    Ruli over 3 years
    Such sulution was already proposed by multiple answers, why to add one more?
  • OctopuSS7
    OctopuSS7 about 3 years
    But this is calculating the whole thing first, so is very resourcce inneficient.
  • Sreekant Shenoy
    Sreekant Shenoy about 3 years
    This works only up to a limit of 15 decimal places since pi value imported from numpy or math consists of 3.141592653589793.
  • Mutex
    Mutex over 2 years
    nope after 48 digits it just shows 0000000000........
  • Jules G.M.
    Jules G.M. over 2 years
    it's in the comment