Project Euler in PL/SQL: Problem 1

  • by

Project Euler is a series of math problems designed to be solved through programming solutions. I’ll be attempting to tackle these problems using PL/SQL and posting my solutions when I solve an answer.

Problem 1:

Multiples of 3 and 5

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.

DECLARE
upper_limit INTEGER := 999;
answer INTEGER := 0;
BEGIN
    FOR i IN  1..upper_limit
    LOOP
        --check if i is divisible by 3 or 5
        IF REMAINDER(i,3) = 0 OR REMAINDER(i,5) = 0
        THEN
            answer := answer + i;
        END IF;
    END LOOP;
 
dbms_output.put_line(answer);
END;

-- output: 233168

A pretty straight forward problem and solution here. I suppose you could create a function for checking the divisibility of each number, but I don’t think the extra code would any any more clarity in this case.

Questions or comments? Feel free to leave them below or reach out to me on Twitter!

Leave a Reply

Your email address will not be published. Required fields are marked *