您的位置:首页 > 其它

Even Fibonacci numbers

2014-03-07 07:20 369 查看
--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.
function list_iter()
local n = 1000000
local i = 1
local j = 0
return function()
if(i < n) then
tmp = i + j
j = i
i = tmp
return i
end
end
end

local sum = 0;
for i in list_iter() do
if(i % 2 == 0) then sum = sum + i end
end
print(sum)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: