我试图通过做各种小项目来学习python,在这种情况下,输入一个数字,pi将计算到该数字输入。在谷歌搜索的一些帮助下,我设法能够计算Pi,但无论我输入什么数字,它仍然会产生相同数量的Pi数字。
我有点困惑在什么地方导致这样做,任何提示都会非常感谢,提前感谢。这是在python 2.7
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
答案 0 :(得分:3)
使用Chudnovsky算法,计算每次迭代产生大约14.18个十进制数字:log10((640320 ^ 3)/(24 * 6 * 2 * 6))〜= 14.18。这可以在本网页所示的 k / a k-1 的公式中更清楚地看到:
https://www.craig-wood.com/nick/articles/pi-chudnovsky
对于n = 5,结果大约有70位精度。
答案 1 :(得分:0)
您可以使用"%.nf"
格式化输出字符串,其中n
是您要输出的位数。
e.g。
import numpy as np
print "%.5f"%(np.pi)
答案 2 :(得分:0)
我只是在您的代码和希望的return语句中添加了舍入函数,它对您来说对我来说是有效的。
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))
答案 3 :(得分:0)
请尝试一下,让我知道它是否对您有用
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)
答案 4 :(得分:0)
我使用数学模块中的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希望对您有所帮助。
答案 5 :(得分:-1)
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))
答案 6 :(得分:-1)
这可能是理解的简单代码
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))
答案 7 :(得分:-1)
这就是我要做的:-)
import math
digits = int(input("to how many digits to you want to round PI?"))
def roundpi(n):
return round(pi,n)
roundpi(digits)
答案 8 :(得分:-1)
#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)
答案 9 :(得分:-2)
from math import pi
num = int(input('Enter the number of decimals: '))
print(f"Pi upto {num}th number is {pi:{1}.{num+1}}")