Lua Implementation of Problem 1

View source code here on GitHub!

Solution

solution()
Returns:

The solution to problem 1

Return type:

number

 1-- Project Euler Question 1
 2--
 3-- I did it the old-fashioned way in this language
 4--
 5-- Problem:
 6--
 7-- If we list all the natural numbers below 10 that are multiples of 3 or 5, we
 8-- get 3, 5, 6 and 9. The sum of these multiples is 23.
 9--
10-- Find the sum of all the multiples of 3 or 5 below 1000.
11
12return {
13    solution = function()
14        local answer = 0
15
16        for i = 3,999,3 do
17            answer = answer + i
18        end
19
20        for i = 5,999,5 do
21            answer = answer + i
22        end
23
24        for i = 15,999,15 do
25            answer = answer - i
26        end
27
28        return answer
29    end
30}

Tags: divisibility