Project Euler in PL/SQL: Problem 6

  • by

Another Project Euler problem that doesn’t prove to be much of a challenge.

Problem 6:

Sum Square Difference

The sum of the squares of the first ten natural numbers is,

12 + 22 + … + 102 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + … + 10)2 = 552 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.


DECLARE
lower_limit PLS_INTEGER := 1;
upper_limit PLS_INTEGER := 100;
sum_of_sq PLS_INTEGER := 0;
sq_of_sum PLS_INTEGER := 0;
BEGIN
    FOR x IN lower_limit..upper_limit
    LOOP
        sum_of_sq := sum_of_sq + x**2;
        sq_of_sum := sq_of_sum + x;
    END LOOP;
    
    sq_of_sum := POWER(sq_of_sum,2);

    dbms_output.put_line(sq_of_sum-sum_of_sq);
END;

--output: 25164150

The ** and POWER operators act exactly the same, I used both just to show they exist.

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 *