new_contents: '''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 itertools
  from collections import namedtuple
  
  Pair = namedtuple('Pair', ['a', 'b'])
  
  def pentagonal_number_gen():
      return (n*(3*n-1)//2 for n in itertools.count(1))
  
  def pentagonal_pairs_gen():
      pentagonal_nums = []
  
      for new in pentagonal_number_gen():
          for old in pentagonal_nums:
              yield Pair(old, new)
          pentagonal_nums.append(new)
  
  def solve_p044():
      candidate = None
      candidate_diff = None
      for pair in pentagonal_pairs_gen():
          diff = pair.b - pair.a
          if is_pentagonal(diff) and is_pentagonal(sum(pair)):
              if not candidate_diff or candidate_diff > diff:
                  candidate = pair
                  candidate_diff = diff
          if candidate and pair.b - candidate.b > candidate_diff:
             return candidate_diff
  
  def is_pentagonal(Pn):
      # Pn=n(3n−1)/2
      # 0 = 3n^2 - n - 2Pn
      # apply quadratic formula..
      return ((1 + (1 + 24*Pn)**.5)/6).is_integer()
  
subject: Solve p044