您的位置:首页 > 其它

Multiples of 3 and 5

2014-03-07 07:22 375 查看
--If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
--Find the sum of all the multiples of 3 or 5 below 1000.

--set up iterator
function list_iter()
local i = 1;
local n = 100;
return function ()
i = i + 1;
if i < n then
return i;
end
end
end

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