Even Fibonacci numbers
1 --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: 2 --1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... 3 --By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. 4 function list_iter() 5 local n = 1000000 6 local i = 1 7 local j = 0 8 return function() 9 if(i < n) then 10 tmp = i + j 11 j = i 12 i = tmp 13 return i 14 end 15 end 16 end 17 18 local sum = 0; 19 for i in list_iter() do 20 if(i % 2 == 0) then sum = sum + i end 21 end 22 print(sum)
posted on 2014-03-07 07:20 Step-BY-Step 阅读(228) 评论(0) 编辑 收藏 举报