Problem 44 : Pentagon numbers

Problem Statement

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 are pentagonal and D = mod(Pk āˆ’ Pj) is minimised; what is the value of D?

Solution

def pentagonal(n):
    if (1+(24*n+1)**0.5) % 6 == 0:
        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 pentagonal(a+b) and pentagonal(a-b):
            print (a-b)
            flag = False
            break
    i += 1

Output

5482660