欧拉计划第二题

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.
翻译下:就是在斐波那契数列中找到小于4000000的那个数,然后求它之前的所有偶数的和并打印出来
even-valued terms 偶数
odd-valued terms 奇数
Ruby代码:

1
2
3
4
5
6
7
8
9
10
11
12
def 
firstNum,secondNum = 0, 1
sumNum = 0

while secondNum < 4000000
firstNum, secondNum = secondNum, firstNum + secondNum

sumNum += secondNum if secondNum % 2 ==0
end
puts "The sum of all even-valued is #{sumNum}"
end
fabonacci()

输出结果:

1
The sum of all even-valued is 4613732