Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
Project Euler 44 Problem Description
Project Euler 44: Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. The first ten pentagonal numbers are:
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, …
It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, 70 − 22 = 48, is not pentagonal.
Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference is pentagonal and D = |Pk − Pj| is minimised; what is the value of D?
When the problem asks for D = |Pk − Pj| to be minimized it simply means the numerically closest pair which, by our method, is the first occurrence found.
We search by sum and difference instead of by Pj and Pk and this caused some confusion. This was the best we we could think of to minimize D without having to force comparisons.
The variable names have been changed to make this more clear:
s is the sum Pk + Pj
Pj is, well, Pj
s-Pj is Pk
D is s-2*Pj which is simplified from (s-Pj)-Pj
The Trinket provided takes about 12 seconds to run.
Project Euler 44 SolutionRuns < 0.210 seconds in Python 2.7.
Use this link to get the Project Euler 44 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.
- See also, Project Euler 45 Solution: Find the next triangle number that is also pentagonal and hexagonal after 40755