Esempio n. 1
0
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?
"""
from Helper import isPentagonal
import sys

def pentagonal(n):
	return n*(3*n-1)/2

k = 2
minD = 10**10
while True:
	for j in range(k-1, 0, -1):
		pk = pentagonal(k)
		pj = pentagonal(j)

		if pk - pj < minD:
			if isPentagonal(pk-pj) and isPentagonal(pk+pj):
				minD = pk-pj
				sys.exit(0)
	k += 1

Esempio n. 2
0
"""
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
Triangle 	  	Tn=n(n+1)/2 	  	1, 3, 6, 10, 15, ...
Pentagonal 	  	Pn=n(3n-1)/2 	  	1, 5, 12, 22, 35, ...
Hexagonal 	  	Hn=n(2n-1) 	  	1, 6, 15, 28, 45, ...

It can be verified that T285 = P165 = H143 = 40755.

Find the next triangle number that is also pentagonal and hexagonal
"""
from Helper import isPentagonal, isHexagonal

n = 286

while True:
	tn = n*(n+1)/2
	if isPentagonal(tn) and isHexagonal(tn):
		print tn
		break
	n += 1