1000 digits of pi in Python

前端 未结 11 1447
忘了有多久
忘了有多久 2020-11-28 10:54

I have been thinking about this issue and I can\'t figure it out. Perhaps you can assist me. The problem is my code isn\'t working to output 1000 digits of pi in the Python

11条回答
  •  时光取名叫无心
    2020-11-28 11:12

    Here is a different way I found here --> Python pi calculation? to approximate python based on the Chudnovsky brothers formula for generating Pi which I have sightly modified for my program.

    def pifunction():
        numberofdigits = int(input("please enter the number of digits of pi that you want to generate"))
        getcontext().prec = numberofdigits
    
    def calc(n):
        t = Decimal(0)
        pi = Decimal(0)
        deno = Decimal(0)
        k = 0
        for k in range(n):
            t = (Decimal(-1)**k)*(math.factorial(Decimal(6)*k))*(13591409+545140134*k)
            deno = math.factorial(3*k)*(math.factorial(k)**Decimal(3))*(640320**(3*k))
            pi += Decimal(t)/Decimal(deno)
        pi = pi * Decimal(12)/Decimal(640320**Decimal(1.5))
        pi = 1/pi
        return str(pi)
    print(calc(1))
    

    I hope this helps as you can generate any number of digits of pi that you wish to generate.

提交回复
热议问题