%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 ++divCounter; 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; clear ans; %Returns result in 7.5 minutes