diff --git a/ProjectEuler/Problem12.m b/ProjectEuler/Problem12.m new file mode 100644 index 0000000..0ab08b7 --- /dev/null +++ b/ProjectEuler/Problem12.m @@ -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