mirror of
https://bitbucket.org/Mattrixwv/octavefunctions.git
synced 2025-12-07 03:03:57 -05:00
Added the first problems for ProjectEuler.net
This commit is contained in:
24
ProjectEuler/Problem2.m
Normal file
24
ProjectEuler/Problem2.m
Normal file
@@ -0,0 +1,24 @@
|
||||
%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
|
||||
Reference in New Issue
Block a user