Project Euler – Problem 6

By | May 25, 2011

Problem 6 from Project Euler:

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.

My solution:

DECLARE
    v_sum PLS_INTEGER := 0;
    v_sum_sq PLS_INTEGER := 0;
    c_limit CONSTANT PLS_INTEGER := 100;
BEGIN
    FOR i in 1 .. c_limit
    LOOP
        v_sum := v_sum + i;
        v_sum_sq := v_sum_sq + (i*i);
    END LOOP;
    dbms_output.put_line('diff = '||to_char(((v_sum*v_sum) - v_sum_sq)));
END;

As always.. I am no mathmatician, so If you know of a better solution please let me know.

Leave a Reply

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

Turn on pictures to see the captcha *