'''
Problem 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 are pentagonal and D = |Pk − Pj| is minimised; what is the value of D?

'''

import numpy as np

def pentagonal(a: int):
    
    pent = lambda x: x*(3*x-1)/2
    arr = np.arange(a)
    i = 0
    while i < len(arr):
        arr[i] = pent(i)
        i += 1

    return arr[arr != 0]


def find_pent(a: int):
    arr = pentagonal(a)
    for i in range(0, len(arr)):
        for j in range(0, len(arr)):
            if arr[i] + arr[j] in arr:
                if arr[j] - arr[i] in arr:
                    return arr[np.where(arr == arr[i] + arr[j])], arr[np.where(arr == arr[j] - arr[i])], arr[i], arr[j], arr[j]-arr[i]
        

def main():
    
    a = 10000
    print('Pj and Pk are: ', find_pent(a))
    #print(distinct_factors(factors(644),644))

if __name__=='__main__':
    main()