mirror of
https://bitbucket.org/Mattrixwv/octavefunctions.git
synced 2025-12-07 03:03:57 -05:00
Added a second chance at Problem 12, more like my C++ code
This commit is contained in:
48
ProjectEuler/Problem12_2.m
Normal file
48
ProjectEuler/Problem12_2.m
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
%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;
|
||||||
Reference in New Issue
Block a user