-
Notifications
You must be signed in to change notification settings - Fork 0
/
p045.py
33 lines (23 loc) · 853 Bytes
/
p045.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Steve Beal
# Project Euler problem 45 solution
# 8/14/15
# Triangle, pentagonal, and hexagonal numbers are generated by the following
# formulae:
# Triangle: T_n = n(n+1)/2 1, 3, 6, 10, 15, ...
# Pentagonal: P_n = n(3n-1)/2 1, 5, 12, 22, 35, ...
# Hexagonal: H_n = n(2n-1) 1, 6, 15, 28, 45, ...
# It can be verified that T_285 = P_165 = H_143 = 40755.
# Find the next triangle number that is also pentagonal and hexagonal.
from utils import triangular_gen, is_pentagonal, is_hexagonal
def next_tri_pent_hex(last_tph = None):
t = triangular_gen()
if last_tph:
# fast-forward the generator to the start point
y = 0
while y < last_tph:
y = next(t)
while True:
x = next(t)
if is_pentagonal(x) and is_hexagonal(x):
return x
print(next_tri_pent_hex(40755))