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

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?


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.


Project Euler 44 Solution last updated


4 Responses to “Project Euler 44 Solution”


    def is_pentagonal(n):
    if (1+(24*n+1)**0.5) % 6 == 0: #function to check if the number is pentagonal number or not
    return True
    return False

    flag = True
    i = 1

    while flag:
    for j in range(1, i):
    a = i*(3*i-1)/2
    b = j*(3*j-1)/2
    if is_pentagonal(a+b) and is_pentagonal(a-b):
    print (a-b)
    flag = False
    i += 1

    Posted by Sinchan | November 2, 2019, 10:26 PM
  2. 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