- 論壇徽章:
- 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.
問(wèn)題2:
斐波那契數(shù)列中的每一項(xiàng)都是由它之前兩項(xiàng)求和得到的。當(dāng)以1和2開(kāi)始時(shí),開(kāi)始10項(xiàng)是:1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ......
現(xiàn)在,把此數(shù)列中的數(shù)值不超過(guò)4000000的項(xiàng)中的偶數(shù)求和,結(jié)果是多少?
代碼:- 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))
- }
復(fù)制代碼 |
|