Example #1
0
#!/usr/bin/python
# coding: UTF-8
"""
@author: CaiKnife

Triangular, pentagonal, and hexagonal
Problem 45
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(3n1)/2     1, 5, 12, 22, 35, ...
Hexagonal       Hn=n(2n1)       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 euler import triangle_number, pentagon_number, hexagon_number

p = set(pentagon_number(i) for i in range(1, 100000))
h = set(hexagon_number(i) for i in range(1, 100000))

for i in range(286, 100000):
    t = triangle_number(i)
    if t in p and t in h:
        print t
        break
Example #2
0
#!/usr/bin/python
# coding: UTF-8
"""
@author: CaiKnife

Pentagon numbers
Problem 44
Pentagonal numbers are generated by the formula, Pn=n(3n1)/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 is pentagonal and D = |Pk  Pj| is minimised; what is the value of D?
"""

from itertools import combinations
from operator import itemgetter, add, sub
from euler import pentagon_number

pentagons = set(pentagon_number(n) for n in range(1, 3000))
c = combinations(pentagons, 2)
for p in c:
    if add(*p) in pentagons and abs(sub(*p)) in pentagons:
        print abs(sub(*p))