【发布时间】:2020-03-22 20:08:31
【问题描述】:
我正在尝试在 1 - 1/3 + 1/5 - 1/7 + 1/9 这样的列表中添加和减去每个数字。如果你继续这样做,然后将答案乘以 4,你会得到 pi 的近似值。
我有一个名为 ODD 的奇数列表,但我无法如上所示进行加减运算
我今天刚开始用 python 编码,所以这可能是一个简单的错误,但我在网上找不到任何关于它的信息
谢谢, 亚当
import time
start_time = time.time()
EVEN = []
ODD = []
y = int(1.e2)
x = range(y)
#-----------------------------------------------------------------------------------------
for i in x:
if i%2 == 0:
print(i)
EVEN.append(i)
else:
print(i)
ODD.append(i)
oc = len(ODD)
ec = len(EVEN)
print("")
print(str(oc) + " " + "Odds Found!")
print(str(ec) + " " + "Evens Found!")
print("--- %s seconds to compute ---" % "{:.2f}".format(((time.time() - start_time))))
time.sleep(3) #START OF PROBLEM
for i in ODD:
fract = 1/ODD[i]-1/ODD[i+1]
sfract = fract + 1/ODD[i+2]
print(fract)
print(sfract)
【问题讨论】:
标签: python math sequence calculator