mirror of
https://bitbucket.org/Mattrixwv/octavefunctions.git
synced 2025-12-06 18:53:57 -05:00
25 lines
672 B
Matlab
25 lines
672 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
|
|
finalSum = 0;
|
|
|
|
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
|
|
|
|
finalSum = sum(evenFib);
|
|
finalSum
|