# coding=utf-8

"""
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 geometry
import utils


def main():
  pents = [n for n in geometry.pent_numbers(10000)]
  for j in xrange(len(pents) - 1):
    for k in xrange(j + 1, len(pents)):
      psum = pents[j] + pents[k]
      pdiff = pents[k] - pents[j]
      if utils.list_has(pents, psum) and utils.list_has(pents, pdiff):
        print abs(pdiff)
        print (pents[j], pents[k], psum, pdiff)


if __name__ == '__main__':
  main()