计算圆周率的第N位数字

3
我想输入一个数字,并计算到该数字的圆周率。我已经能够计算出圆周率,但无论我输入什么数字,它都会生成相同数量的圆周率数字。我有点困惑在哪个环节导致了这种情况。
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

1
你为什么认为它会产生“n”个小数?就我所看到的,它将在更高的“n”下产生更好的结果,但肯定不是完全准确的“n”位数字。而且,如果你改变“n”,结果也会不同。 - klutt
不分析代码:你的输出是什么意思?请举个例子!有可能,输出只是一个浮点数打印,因此看到的数字位数并不取决于你计算了多少位数字?请详细说明。 - sascha
要明确的是,无论您选择什么n,您都将输出相同数量的数字,但是使用更高的n会使结果更准确。 - klutt
更新了我的输出,我想我有点困惑,不知道如何让我的函数根据输入执行计算。 - Blooze
10个回答

8
使用Chudnovsky算法,每次迭代可以产生约14.18位小数:log10((640320^3)/(24*6*2*6)) ~= 14.18。这在ak / ak-1的公式中更清晰地展示在此网页上:https://www.craig-wood.com/nick/articles/pi-chudnovsky
当n = 5时,结果精度约为70位小数。

2
我在你的代码中添加了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))

但是这会先计算整个事情,因此非常浪费资源。 - OctopuSS7

0

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

import numpy as np
print "%.5f"%(np.pi)

1
不适用于大于50的N值 3.14159265358979311599796346854418516159057617187500000000000000000 - Luo Hongshuai

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

除了代码之外,您还应该添加一个关于您提出的解决方案的描述。 - il_raffa
it's in the comment - Jules G.M.

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

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

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

-1
请尝试这个方法,如果对你有用的话,请告诉我。
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 的话。 - PirateNinjas
这仅适用于小数点后15位的限制,因为从numpy或math导入的“pi”值包括“3.141592653589793”。 - Sreekant Shenoy

-1

我用数学模块中的 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的代码,希望能对您有所帮助。


48位数字后,它只会显示0000000000......。 - nTheta

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

这种解决方案已经被多个答案提出了,为什么还要再添加一个呢? - Ruli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接