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!