__author__ = 'karthikb'

'''
Pentagon numbers are generated by Pn = n(3n-1)/2
Find two numbers whose sum and difference is also pentagonal and difference is minimized
'''
from cmath import sqrt
def solve_quadratic(a,b,c):
    '''solving quadratic aX2+bx+c = 0'''

    d = b**2-4*a*c
    if d> 0:

        x1 = (-b+sqrt(d).real)/(2*a)
        x2 = (-b-sqrt(d).real)/(2*a)
    else:
        x1 = (-b+sqrt(d).imag)/(2*a)
        x2 = (-b-sqrt(d).imag)/(2*a)

    return x1,x2

print solve_quadratic(3,-1,-12)

def check_validity((x1,x2)):
    return x1 % 1==0 or x2 % 1==0




def find_pentagonal():
    n = 1
    a = 3
    b = -1

    p_num = []
    while True:
        Pn =  n*(3*n-1)//2

        print n,Pn
        for i in p_num:
            diff = abs(Pn - i)
            add = Pn+i
            c = - 2 *add
            if check_validity(solve_quadratic(a,b,c)) and diff in p_num:
                return diff,add,Pn,i
        p_num.append(Pn)
        n = n +1



print find_pentagonal()

