尝试在python中实现Nilakantha算法

尝试在python中实现Nilakantha算法,python,python-3.x,algorithm,pi,Python,Python 3.x,Algorithm,Pi,它总是返回39983.4247879279112354716423 知道这为什么不起作用吗?注意分母中的括号,每轮更新两次值 import math from decimal import * pi = Decimal(3) two = 2 three = 3 four = 4 times = 10000 for c in range(times): pi += Decimal(4)/two * three * four pi -= Decimal(4)/two + 2

它总是返回39983.4247879279112354716423
知道这为什么不起作用吗?

注意分母中的括号,每轮更新两次值

import math
from decimal import *

pi = Decimal(3)

two = 2
three = 3
four = 4

times = 10000

for c in range(times):
    pi += Decimal(4)/two * three * four
    pi -= Decimal(4)/two + 2 * three + 2 * four + 2 * three

    two += 2
    three += 2
    four += 4

print(pi)

您试图实现什么算法/公式?这应该是你问题的一部分。另外,请注意,
10/2*5
是25,而不是1。@cantevendot这是你知道将答案标记为解决方案吗?
for c in range(times):
    pi += Decimal(4)/(two * three * four)
    two += 2
    three += 2
    four += 2
    pi -= Decimal(4)/(two * three * four)
    two += 2
    three += 2
    four += 2