欧拉工程-问题2
文章目錄
原题地址 http://projecteuler.net/problem=2
Even Fibonacci numbers
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.
偶数斐波纳契数
斐波那契数列中的每一个数等于前面两个数的和,从1和2开始,前面十项为:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89,。。。
考虑斐波那契数中不超过4000000的数,求这些数中所有偶数的和。
解法:
这题不多说,关键是生成斐波那契数列,这里使用了一个迭代器的技巧。代码如下:
1 | #!/usr/bin/python |