Monthly Archives: May 2011

Project Euler – Problem 6

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.

Project Euler – Problem 5

Problem 5 from Project Euler:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

My solution:

DECLARE
c_step PLS_INTEGER := 20;
v_sn PLS_INTEGER := c_step;
aed BOOLEAN := FALSE;
ed BOOLEAN;
BEGIN
WHILE (NOT aed)
LOOP
ed := TRUE;
FOR i in 2..c_step
LOOP
IF mod(v_sn,i) <> 0
THEN
ed := FALSE;
EXIT;
END IF;
END LOOP;
IF ed
THEN
aed := TRUE;
ELSE
v_sn := v_sn + c_step;
END IF;
END LOOP;
dbms_output.put_line('sn: '||v_sn);
END;

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

Project Euler – Problem 4

Problem 4 from Project Euler:

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.

Find the largest palindrome made from the product of two 3-digit numbers.

My solution:

DECLARE
v_p PLS_INTEGER;

FUNCTION is_palendrome(
num_in IN NUMBER
)
RETURN BOOLEAN
IS
v_val VARCHAR2(255) := to_char(num_in);
v_ret BOOLEAN := TRUE;
BEGIN
FOR i in 1..ceil(length(v_val)/2)
LOOP
IF substr(v_val, i, 1) <> substr(v_val, -1*i, 1)
THEN
v_ret := FALSE;
END IF;
END LOOP;
RETURN v_ret;
END is_palendrome;

FUNCTION largest_palendrome(
num_digits_in IN PLS_INTEGER
)
RETURN PLS_INTEGER
IS
v_max PLS_INTEGER := to_number(rpad('9',num_digits_in, '9'));
v_lp PLS_INTEGER := 0;
BEGIN
FOR i in 1 .. v_max
LOOP
FOR j in 1 .. v_max
LOOP
IF is_palendrome(i*j) and i*j > v_lp
THEN
v_lp := i*j;
END IF;
END LOOP;
END LOOP;
RETURN v_lp;
END largest_palendrome;
BEGIN
dbms_output.put_line('lp: '||largest_palendrome(3));
END;

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

Project Euler – Problem 3

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!

Project Euler – Problem 2

Problem 2 from Project Euler:

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

My solution:

DECLARE
v_prev PLS_INTEGER := 1;
v_curr PLS_INTEGER := 2;
v_tmp PLS_INTEGER;
v_sum PLS_INTEGER := 0;
BEGIN
WHILE v_curr < 4000000
LOOP
IF mod(v_curr, 2) = 0
THEN
v_sum := v_sum + v_curr;
END IF;
--
v_tmp := v_curr;
v_curr := v_curr + v_prev;
v_prev := v_tmp;
END LOOP;
dbms_output.put_line('sum: '||v_sum);
END;

Again, I am no mathmatician, so If you know of a better solution please let me know!

Project Euler – Problem 1

Problem 1 from Project Euler:

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.

My solution:

DECLARE
v_sum PLS_INTEGER := 0;
c_max CONSTANT PLS_INTEGER := 1000;
BEGIN
FOR i IN 1 .. c_max - 1
LOOP
IF mod(i,3) = 0 or mod(i,5) = 0
THEN
v_sum := v_sum + i;
END IF;
END LOOP;
dbms_output.put_line('sum: '||v_sum);
END;

Again, I am no mathmatician, so If you know of a better solution please let me know!

Project Euler Problems

One of my colleagues introduced me to Project Euler. From their website:

What is Project Euler?
Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

The motivation for starting Project Euler, and its continuation, is to provide a platform for the inquiring mind to delve into unfamiliar areas and learn new concepts in a fun and recreational context.

I thought these were interesting problems, so I thought I would give them a go. Since PL/SQL is my language of choice, I figured I would see how many I could do using SQL or PL/SQL. I will post my solutions when I get time. I am no mathmatician, so there will likely be much more elegant ways to solve these problems, but I am always up for a challenge so it should still be fun. If you know of a better solution for any of them by all means let me know!