Project Euler – Problem 3

By | May 17, 2011

Project Euler – Problem 3

Problem 3 from Project Euler:

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

My solution:

DECLARE
    v_pf BINARY_INTEGER;
    c_num CONSTANT NUMBER := 600851475143;
    v_is_prime BOOLEAN := TRUE;
BEGIN
    FOR i IN 2..floor(sqrt(c_num))
    LOOP
        IF mod(c_num,i) = 0
        THEN
            FOR j in 2..floor(sqrt(i))
            LOOP
                IF mod(i,j) = 0
                THEN
                    v_is_prime := FALSE;
                    exit;
                END IF;
            END LOOP;
            IF v_is_prime
            THEN
                v_pf := i;
            ELSE
                v_is_prime := TRUE;
            END IF;            
        END IF;
    END LOOP;
    dbms_output.put_line('largest prime: '||v_pf);
END;

Again, 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 *