- 论坛徽章:
- 4
|
本帖最后由 icymirror 于 2015-09-25 09:42 编辑
Problem 2:
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.
问题2:
斐波那契数列中的每一项都是由它之前两项求和得到的。当以1和2开始时,开始10项是:1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ......
现在,把此数列中的数值不超过4000000的项中的偶数求和,结果是多少?
代码:- package main
- import (
- "fmt"
- )
- func Problem002(scope int) int {
- sum := 0
- first := 1
- second := 2
-
- for {
- if (second > scope) {
- break
- }
-
- sum = sum + second
- first, second = first + second * 2, first * 2 + second * 3
- }
-
- return sum
- }
- func main() {
- fmt.Println("Problem 002 result: ", Problem002(4000000))
- }
复制代码 |
|