Files
OctaveFunctions/ProjectEuler/Problem2.m

28 lines
706 B
Matlab

%ProjectEuler/Problem2.m
%This is a script to answer Problem 2 for Project Euler
%The sum of the even Fibonacci numbers less than 4,000,000
%Setup your Variables
fib = [1, 1, 2]; %Holds the Fibonacci numbers
currentFib = fib(end) + fib(end - 1); %The current Fibonacci number to be tested
evenFib = [2]; %A subset of the even Fibonacci numbers
while(currentFib < 4000000)
%Add the number to the list
fib(end + 1) = currentFib;
%If the number is even add it to the even list as well
if(mod(currentFib, 2) == 0)
evenFib(end + 1) = currentFib;
end
%Set the next Fibonacci
currentFib = fib(end) + fib(end - 1);
end
sum(evenFib)
%Cleanup your variables
clear fib;
clear currentFib;
clear evenFib;