mirror of
https://bitbucket.org/Mattrixwv/octavefunctions.git
synced 2025-12-06 18:53:57 -05:00
Added the answer to Problem 12
This commit is contained in:
51
ProjectEuler/Problem12.m
Normal file
51
ProjectEuler/Problem12.m
Normal file
@@ -0,0 +1,51 @@
|
||||
%ProjectEuler/Problem12.m
|
||||
%This is a script to answer Problem 12 for Project Euler
|
||||
%What is the value of the first triangle number to have over five hundred divisors?
|
||||
|
||||
%Setup your variables
|
||||
counter = 3500; %To hold the current number you have counted up to
|
||||
number = 0; %To hold the current triangle number
|
||||
found = false; %To tell when the answer has been found
|
||||
numDivisors = 0;
|
||||
maxDivisors = 0;
|
||||
maxCounter = 0;
|
||||
|
||||
|
||||
startTime = clock();
|
||||
while(~found)
|
||||
%Get your next triangle number
|
||||
number = sym(sum([1:counter]))
|
||||
|
||||
%See if it has 500 divisors
|
||||
numDivisors = size(divisors(number))(2)
|
||||
if(numDivisors > maxDivisors)
|
||||
maxDivisors = numDivisors
|
||||
maxCounter = counter
|
||||
end
|
||||
if(numDivisors > 500)
|
||||
found = true;
|
||||
else
|
||||
counter = counter + 1
|
||||
end
|
||||
end
|
||||
endTime = clock();
|
||||
|
||||
%Print your result
|
||||
totalTime = etime(endTime, startTime)
|
||||
topNumber = counter
|
||||
double(number)
|
||||
|
||||
%Cleanup your variables
|
||||
clear counter;
|
||||
clear number;
|
||||
clear found;
|
||||
clear topNumber;
|
||||
clear startTime;
|
||||
clear endTime;
|
||||
clear totalTime;
|
||||
clear numDivisors;
|
||||
clear maxDivisors;
|
||||
clear maxCounter;
|
||||
|
||||
%This will take 6-7 hours to run. I got the C++ answer in 2.8 seconds
|
||||
%Try to find a more efficient way to run this
|
||||
Reference in New Issue
Block a user