# Pentagonal numbers are generated by the formula, P_n = 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 P_4 + P_7 = 22 + 70 = 92 = P_8. However, their
# difference, 70 - 22 = 48, is not pentagonal.

# Find the pair of pentagonal numbers, P_j and P_k, for which their
# sum and difference are pentagonal and D = |P_k - P_j| is minimised;
# what is the value of D?

from math import sqrt

def is_pentagonal(num):
	tmp = (sqrt(24 * num + 1) + 1) / 6
	return tmp == int(tmp)

i = 1
isFound = False
while not isFound:
	p1 = i * (3 * i - 1) / 2
	for j in range(i - 1, 0, -1):
		p2 = j * (3 * j - 1) / 2
		if is_pentagonal(p1 + p2) and is_pentagonal(p1 - p2):
			isFound = True
			print p1 - p2
			break
	i += 1

c2907f389d926f6e3727a44cacbfe3ba026ab527
# Pentagonal numbers are generated by the formula, P_n = 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 P_4 + P_7 = 22 + 70 = 92 = P_8. However, their
# difference, 70 - 22 = 48, is not pentagonal.

# Find the pair of pentagonal numbers, P_j and P_k, for which their
# sum and difference are pentagonal and D = |P_k - P_j| is minimised;
# what is the value of D?

from math import sqrt

def is_pentagonal(num):
	tmp = (sqrt(24 * num + 1) + 1) / 6
	return tmp == int(tmp)

i = 1
isFound = False
while not isFound:
	p1 = i * (3 * i - 1) / 2
	for j in range(i - 1, 0, -1):
		p2 = j * (3 * j - 1) / 2
		if is_pentagonal(p1 + p2) and is_pentagonal(p1 - p2):
			isFound = True
			print p1 - p2
			break
	i += 1
