// you’re reading...
1 Star2 Stars3 Stars4 Stars5 Stars (31 votes, average: 4.84 out of 5)
Loading...

Project Euler Solutions

Project Euler 44 Solution

Project Euler 44 Solution

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?

Analysis

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 Solution

Runs < 0.210 seconds in Python 2.7.
download arrowUse this link to get the Project Euler 44 Solution Python 2.7 source.

Answer

Slowly 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.
|5482660|

Afterthoughts

Project Euler 44 Solution last updated

Discussion

3 Responses to “Project Euler 44 Solution”

  1. In the comparison you tested if p-n and p-2*n belonged to the set of pentagonals. why not p+n?

    Posted by Marcio | December 17, 2015, 3:42 AM

Post a comment