简体   繁体   English

我在 Wallis 产品上的 python 代码有什么问题?

[英]What is wrong with my python code on Wallis product?

So, I was asked to code the Wallis product and it wasn't supposed to be really complicated.所以,我被要求编写 Wallis 产品的代码,它本不应该很复杂。 So I made a code but apparently, it could only work for Wallis(1) and not the rest.所以我做了一个代码,但显然,它只适用于Wallis(1)而不是其他的。 Could anyone help me?有人可以帮助我吗? Thank you!谢谢!

def Wallis (n):
    result = 1
    for count in range(2, n+2, 2):
        result = result * (count**2/((count-1)*(count+1)))
    return result

Formula to compute Wallis product计算 Wallis 乘积的公式

(2*2)/(3*5) * (4*4)/(5*7) * (6*6)/(7*9) and so on until ((n*2) ** 2)/ ((n-1)* (n+1))

Output输出

Wallis(1) = (2*2)/(3*5) = 0.267
Wallis(2) = Wallis(1) * (4*4)/(5*7) = 0.122

Mainly your range command is wrong:主要是你的 range 命令是错误的:

range(2, 2*n+2, 2)

Alternatively, you can move the complexity to the formula, ie:或者,您可以将复杂性移至公式,即:

for count in range(1, n):
    result = result * (4*count*count/((2*count-1)*(2*count+1)))

The problem with your code is that count is equal to 2*n in your equation so in (count**2/((count-1)*(count+1))) , count**2 is the same as (2*n)**2 however the following count-1 should be the same as n-1 but rather it is actually (2*n)-1 .您的代码的问题在于,在等式中count等于2*n ,因此在(count**2/((count-1)*(count+1)))count**2(2*n)**2但是以下count-1应该与n-1相同,但实际上是(2*n)-1 The same goes for count+1 . count+1

I made my own version which should help you (although I used a different equation which is at https://www.wikiwand.com/en/Wallis_product )我制作了自己的版本,应该可以帮助你(虽然我使用了一个不同的方程式,它位于https://www.wikiwand.com/en/Wallis_product

def wallis(limit):
    result = 1
    for x in range(2, limit, 2):
        result *= (x / (x - 1)) * (x / (x + 1))
    return result

As limit gets higher, it converges closer to half pi.随着limit变得更高,它会收敛到接近 pi 的一半。

As others said, your range() needs to be changed.正如其他人所说,您的range()需要更改。

For n=3,对于 n=3,

for count in range(2, n+2, 2):
    print(i)

would print会打印

2
4

but you needed 6 as well.但你也需要6

As Python documentation says正如 Python 文档所说

class range(start, stop[, step]) 

For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop.对于正步,范围 r 的内容由公式 r[i] = start + step*i 确定,其中 i >= 0 且 r[i] < stop。

meaning stop is not included.意思是不包括stop

Here step is 2 , you could use这里的step2 ,你可以使用

for count in range(2, 2*(n+1), 2):

And exponentiation is costlier than multiplication.求幂比乘法更昂贵。 So所以

count*count

is better than

count**2

So the modified version could be所以修改后的版本可能是

def Wallis(n):
    result=1
    for count in range(2, 2*(n+1), 2):
        result*=((count*count)/((count-1)*(count+1)))
    return result

Note that the value returned by Wallis is only half the value of Pi.注意Wallis返回的值只是 Pi 值的一半。

You could modify the return statement to您可以将return语句修改为

return result*2

if you want.如果你想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM