mirror of
https://bitbucket.org/Mattrixwv/projecteuleroctave.git
synced 2025-12-06 17:43:57 -05:00
Changed script to function
This commit is contained in:
59
Problem2.m
59
Problem2.m
@@ -1,10 +1,11 @@
|
|||||||
|
function [] = Problem2()
|
||||||
%ProjectEuler/Octave/Problem2.m
|
%ProjectEuler/Octave/Problem2.m
|
||||||
%Matthew Ellison
|
%Matthew Ellison
|
||||||
% Created:
|
% Created: 03-28-19
|
||||||
%Modified: 03-28-19
|
%Modified: 10-28-20
|
||||||
%The sum of the even Fibonacci numbers less than 4,000,000
|
%The sum of the even Fibonacci numbers less than 4,000,000
|
||||||
%{
|
%{
|
||||||
Copyright (C) 2019 Matthew Ellison
|
Copyright (C) 2020 Matthew Ellison
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU Lesser General Public License as published by
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
@@ -21,42 +22,36 @@
|
|||||||
%}
|
%}
|
||||||
|
|
||||||
|
|
||||||
%Setup your Variables
|
%Setup your Variables
|
||||||
fib = [1, 1, 2]; %Holds the Fibonacci numbers
|
fib = [1, 1, 2]; %Holds the Fibonacci numbers
|
||||||
currentFib = fib(end) + fib(end - 1); %The current Fibonacci number to be tested
|
currentFib = fib(end) + fib(end - 1); %The current Fibonacci number to be tested
|
||||||
evenFib = [2]; %A subset of the even Fibonacci numbers
|
evenFib = [2]; %A subset of the even Fibonacci numbers
|
||||||
|
|
||||||
%Start the timer
|
%Start the timer
|
||||||
startTime = clock();
|
startTime = clock();
|
||||||
|
|
||||||
while(currentFib < 4000000)
|
while(currentFib < 4000000)
|
||||||
%Add the number to the list
|
%Add the number to the list
|
||||||
fib(end + 1) = currentFib;
|
fib(end + 1) = currentFib;
|
||||||
%If the number is even add it to the even list as well
|
%If the number is even add it to the even list as well
|
||||||
if(mod(currentFib, 2) == 0)
|
if(mod(currentFib, 2) == 0)
|
||||||
evenFib(end + 1) = currentFib;
|
evenFib(end + 1) = currentFib;
|
||||||
|
end
|
||||||
|
|
||||||
|
%Set the next Fibonacci
|
||||||
|
currentFib = fib(end) + fib(end - 1);
|
||||||
end
|
end
|
||||||
|
|
||||||
%Set the next Fibonacci
|
%Stop the timer
|
||||||
currentFib = fib(end) + fib(end - 1);
|
endTime = clock();
|
||||||
|
|
||||||
|
%Print the results
|
||||||
|
printf("The sum of all even Fibonacci numbers less than 4000000 is %d\n", sum(evenFib))
|
||||||
|
printf("It took %f seconds to run this algorithm\n", etime(endTime, startTime))
|
||||||
end
|
end
|
||||||
|
|
||||||
%Stop the timer
|
|
||||||
endTime = clock();
|
|
||||||
|
|
||||||
%Print the results
|
|
||||||
printf("The sum of all even Fibonacci numbers less than 4000000 is %d\n", sum(evenFib))
|
|
||||||
printf("It took %f seconds to run this algorithm\n", etime(endTime, startTime))
|
|
||||||
|
|
||||||
%Cleanup your variables
|
|
||||||
clear fib;
|
|
||||||
clear currentFib;
|
|
||||||
clear evenFib;
|
|
||||||
clear startTime;
|
|
||||||
clear endTime;
|
|
||||||
|
|
||||||
%{
|
%{
|
||||||
Results:
|
Results:
|
||||||
The sum of all even Fibonacci numbers less than 4000000 is 4613732
|
The sum of all even Fibonacci numbers less than 4000000 is 4613732
|
||||||
It took 0.001076 seconds to run this algorithm
|
It took 0.000694 seconds to run this algorithm
|
||||||
%}
|
%}
|
||||||
|
|||||||
Reference in New Issue
Block a user