#!/usr/bin/env python3

# -*- coding: utf-8 -*-
"""
@author: alexogilvie

Project Euler Problem 44: Pentagon numbers

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 time
from math import inf, sqrt

def isPentagonal(n):
    return (sqrt(6*n + 1/4) / 3 + 1/6).is_integer()

def compute():
    timer = time.time()
    pentagon = [n * (3 * n - 1)//2 for n in range(10000)]
    ans = inf
    for i in range(1,len(pentagon)):
        for j in range(i,len(pentagon)):
            if isPentagonal(abs(pentagon[i] - pentagon[j])) and isPentagonal(pentagon[i] + pentagon[j]) and abs(pentagon[j] - pentagon[i]) < ans:
                ans = abs(pentagon[j] - pentagon[i])
    print ("Answer is " + str(ans) + ". Completed in "+str(time.time()-timer)+" seconds.")
    return ans
"""
3/2 n^2 - 1/2 n - c = 0

1/6 +- sqrt(1/4 + 6c) / 3

"""