# 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. # answer : 4613732 # def fibonacci(number) a = 1 b = 2 c = 0 sum = 2 loop do c = a+b a = b b = c break if c > number.to_i sum +=c if(c%2==0) end puts sum end fibonacci(4000000)
ProjectRuler-2
最新推荐文章于 2011-11-04 12:45:45 发布