mirror of
https://bitbucket.org/Mattrixwv/projecteuleroctave.git
synced 2025-12-06 17:43:57 -05:00
66 lines
1.8 KiB
Matlab
66 lines
1.8 KiB
Matlab
%ProjectEuler/Octave/Problem1.m
|
|
%Matthew Ellison
|
|
% Created:
|
|
%Modified: 03-28-19
|
|
%What is the sum of all the multiples of 3 or 5 that are less than 1000
|
|
%{
|
|
Copyright (C) 2019 Matthew Ellison
|
|
|
|
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
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
%}
|
|
|
|
|
|
%Setup your variables
|
|
fullSum = 0; %To hold the sum of all the numbers
|
|
numbers = 0; %To hold all of the numbers
|
|
counter = 0; %The number. It must stay below 1000
|
|
|
|
%Start the timer
|
|
startTime = clock();
|
|
|
|
%When done this way it removes the possibility of duplicate numbers
|
|
while(counter < 1000)
|
|
%See if the number is a multiple of 3
|
|
if(mod(counter, 3) == 0)
|
|
numbers(end + 1) = counter;
|
|
%See if the number is a multiple of 5
|
|
elseif(mod(counter, 5) == 0)
|
|
numbers(end + 1) = counter;
|
|
end
|
|
|
|
%Increment the number
|
|
++counter;
|
|
end
|
|
|
|
%Stop the timer
|
|
endTime = clock();
|
|
|
|
%Print the results
|
|
printf("The sum of all the numbers less than 1000 that is divisibly by 3 or 5 is: %d\n", sum(numbers))
|
|
printf("It took %f seconds to run this algorithm\n", etime(endTime, startTime))
|
|
|
|
%Cleanup your variables
|
|
clear fullSum;
|
|
clear numbers;
|
|
clear counter;
|
|
clear startTime;
|
|
clear endTime;
|
|
clear ans;
|
|
|
|
%{
|
|
Results:
|
|
The sum of all the numbers less than 1000 that is divisibly by 3 or 5 is: 233168
|
|
It took 0.014717 seconds to run this algorithm
|
|
%}
|