Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23305

Calculating Pi to the Nth digit

$
0
0

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 factorialfrom decimal import Decimal, getcontext# Chudnovsky algorithm for figuring out pigetcontext().prec=100pi_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 piprint calc(n)

Here is my output

How many digits of pi would you like? 5 3.14159265358979323846264338327950288419716939937510582097494459230781634694690247717268165239156011

Viewing all articles
Browse latest Browse all 23305

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>