我的问题是:为什么这两个程序提供不同的答案!先感谢您! 我创建了两个程序,根据总和的用户输入,尽可能接近pi的值。但是,我创建了两个程序,一个步骤2,一个步骤4步。我想知道为什么两个代码提供的答案是不同的。 使用2步的代码。
import math
total = 0
def main():
#input
n = int(input("How many numbers are we going to process: "))
#process
#initialize total
total = 0
#create the loop that runs via amount input
for i in range(0, n , 2):
total = total + ( 1 / ((i * 2) + 1)) - (1 / ((i * 2) + 3))
amount = total * 4
print(amount)
print("In comparison to pi the value is: ", (math.pi - amount))
main()
使用4步的代码
import math
total = 0
def main():
#input
n = int(input("How many numbers are we going to process: "))
#process
#initialize total
total = 0
#create the loop that runs via amount input
for i in range(0, n , 4):
total = total + ( 1 / ((i * 2) + 1)) - (1 / ((i * 2) + 3)) + (1/
((i * 2) + 5)) - (1/ ((i * 2) + 7))
amount = total * 4
print(amount)
print("In comparison to pi the value is: ", (math.pi - amount))
main()