Tuesday, June 15, 2010

Euler Problem 1

Problem:

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.


Solution:

function euler1 (range)
local result = 0

for i=3,range do
if (i % 3) == 0 or (i % 5) == 0 then
result = result + i
end
end

return result
end

print(euler1(999))


There's really nothing clever here, I just used the most straightforward way of accumulating the results imperatively. Mostly boring stuff, but now we are underway!

No comments: