Multiples of 3 and 5

 1 --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.
 2 --Find the sum of all the multiples of 3 or 5 below 1000.
 3 
 4 --set up iterator
 5 function list_iter()
 6     local i = 1;
 7     local n = 100;
 8     return function ()
 9         i = i + 1;
10         if i < n then
11             return i;
12         end
13     end
14 end
15 
16 local sum = 0;
17 for i in list_iter() do
18     if(i % 3 == 0 or i % 5 == 0) then
19         print(i);
20         sum = sum + i;
21     end
22 end
23 print(sum);

 

posted on 2014-03-07 07:22  Step-BY-Step  阅读(255)  评论(0编辑  收藏  举报

导航