Project Euler 12: Find the value of the first triangle number to have over five hundred divisors
Project Euler 12 Problem Description
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …
Let us list the factors of the first seven triangle numbers:
We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors?
Our algorithm for solving this one was improved nicely by a detailed explanation provided by a Project Euler moderator named ‘rayfil’. His wonderful explanation is published as a PDF document that is available after you solve the problem. What follows is our own interpretation written in Python.
If you run the Trinket for an input of 500 it will take about 15 seconds to run.
Project Euler 12 SolutionRuns < 0.113 seconds in Python 2.7.
Use this link to get the Project Euler 12 Solution Python 2.7 source.
AnswerSlowly swipe from either end beginning with the white vertical bar to get an idea of the starting or ending digits. For less drama, just double click the answer area. The distance between the two bars will give you an idea of the magnitude. Touch devices can tap and hold the center of the box between the two bars and choose define to reveal the answer.
- This is the second incarnation of this solution, the first taking longer and more complicated to understand. This version also passes all the HackerRank test problems.