mirror of
https://bitbucket.org/Mattrixwv/octavefunctions.git
synced 2025-12-07 03:03:57 -05:00
49 lines
930 B
Matlab
49 lines
930 B
Matlab
%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?
|
|
|
|
found = false;
|
|
numSum = 1;
|
|
counter = 2;
|
|
numDivisors = 0;
|
|
goalDivisors = 500;
|
|
|
|
startTime = clock();
|
|
while(~found)
|
|
%Count the number of divisors
|
|
numDivisors = 0;
|
|
divCounter = 0;
|
|
while((divCounter * divCounter) < numSum)
|
|
if(mod(numSum, divCounter) == 0)
|
|
numDivisors += 2;
|
|
end
|
|
end
|
|
|
|
%Check if there are enough divisors
|
|
if(numDivisors > goalDivisors)
|
|
found = true;
|
|
else
|
|
numSum += counter;
|
|
++counter;
|
|
end
|
|
end
|
|
endTime = clock();
|
|
|
|
%Print the result
|
|
number = numSum
|
|
highestNumber = counter - 1
|
|
numDivisors = numDivisors
|
|
totalTime = etime(endTime - startTime)
|
|
|
|
%Cleanup the variables
|
|
clear found;
|
|
clear numSum;
|
|
clear counter;
|
|
clear numDivisors;
|
|
clear goalDivisors;
|
|
clear startTime;
|
|
clear endTime;
|
|
clear totalTime;
|
|
clear numDivisors;
|
|
clear divCounter;
|