将 pi 计算到第 n 位数字



我正在尝试输入一个数字并计算该数字输入的pi。我设法能够计算 Pi,但是无论我输入什么数字,它仍然会生成相同数量的 Pi 数字。

我有点困惑它在什么时候导致这样做

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)

这是我的输出

How many digits of pi would you like? 5 
3.141592653589793238462643383279502884197169399375105820974944592307816346
94690247717268165239156011

使用 Chudnovsky 算法,计算每次迭代产生大约 14.18 个十进制数字:log10((640320^3)/(24*6*2*6)) ~= 14.18。这可以在k/ak-1的公式中更清楚地看到,如本网页所示:

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

对于 n = 5,结果具有大约 70 位精度。

我刚刚在您的代码和希望的 return 语句中添加了 round 函数,它对您和我一样有效。

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))

您可以使用"%.nf"来格式化输出字符串,其中n是要输出的位数。 例如

import numpy as np
print "%.5f"%(np.pi)
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))

这可能是一个简单的理解代码

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))

这就是我会这样做的方式:-(

import math
digits = int(input("to how many digits to you want to round PI?"))
def roundpi(n):
return round(pi,n)
roundpi(digits)

请尝试此操作,并让我知道它是否适合您

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)    

我使用数学模块中的 PI 简短地回答了同样的问题。

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))

输出

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

这是在python 3.8中,希望它有所帮助。

#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)

from math import pi
num = int(input('Enter the number of decimals: '))
print(f"Pi upto {num}th number is {pi:{1}.{num+1}}")

最新更新