You should only run the loop n - 1
times. Because the assignment a = 0
initializes x1
. Then after each loop, a
is assigned by the next value (from x2
)
def loop(n):
a = 0
b = 1
for i in range(n - 1):
a, b = b, 2*b - a
return a
print(loop(30))
# 29
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…