# Project Euler: Problem 44
# Jonathan Kosasih

#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 = |Pk − Pj| is minimised; what is the value of D?
#------------------------------------------------------------------
# wikipedia pentagonal numbers, check if n is a pentagonal number by if (1+(24n+1)^(1/2))/6 is an integer
def answer():
    ans = []
    i = 1
    flag = True
    while(flag):
        for j in range(1,i):
            a = i*(3*i-1)/2
            b = j*(3*j-1)/2
            if(isPentagonal(a+b) and isPentagonal(a-b)):
                ans = int(abs(a-b))
                flag = False
                break
        i += 1
    return ans

def isPentagonal(n):
    if((1+(24*n+1)**(.5)) % 6 == 0):
        return True
    return False
        
ans = answer()
print(str(ans))