## 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, P_{n}=`n`(3`n`−1)/2. The first ten pentagonal numbers are:

1, 5, 12, 22, 35, 51, 70, 92, 117, 145, …

It can be seen that P_{4} + P_{7} = 22 + 70 = 92 = P_{8}. However, their difference, 70 − 22 = 48, is not pentagonal.

Find the pair of pentagonal numbers, P_{j} and P_{k}, for which their sum and difference is pentagonal and D = |P_{k} − P_{j}| is minimised; what is the value of D?

#### Analysis

When the problem asks for D = |P_{k} − P_{j}| 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 P_{j} and P_{k} 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 P_{k} + P_{j}

`Pj` is, well, P_{j}

`s-Pj` is P_{k}

`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.Use this link to get the Project Euler 44 Solution Python 2.7 source.

#### Afterthoughts

- See also, Project Euler 45 Solution:

*Project Euler 44 Solution last updated*

MY SOLUTION HARDLY TAKES 1.5sec

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

break

i += 1

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

Because we are searching for the sum and D, not Pk and Pj. I revised the description to make this more clear.