################################################################################ #Question: # #Each new term in the Fibonacci sequence is generated by adding # #the previous two terms. By starting with 1 and 2, the first 10 terms will be: # # # # 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... # # # #By considering the terms in the Fibonacci sequence whose values do not exceed # #four million, find the sum of the even-valued terms # #By IT05 2012-08-14 ################################################################################ #one s = [1,2] while s[-1]<=4000000: s.append(s[-1]+s[-2]) print(sum([i for i in s if i%2==0])) #two b = [3,5] s = [2] while s[-1]<=4000000: s.append(sum(b)) b = [b[1]+s[-1],b[1]+s[-1]+s[-1]] s.pop() print(sum(s))
for i in s: if i%2==1: s.remove(i) print('remove:'+str(i))
于是用这段代码,然后想通过直接执行
print(sum(s))
s = [1,2] while s[-1]<=4000000: s.append(s[-1]+s[-2]) print(s) #循环1 for i in s: if i%2==1: s.remove(i) print(s) #循环2 for i in s: if i%2==1: s.remove(i) print(s)