mirror of
https://github.com/correl/euler.git
synced 2024-11-30 11:09:52 +00:00
54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
|
"""After 40755, what is the next triangle number that is also pentagonal and hexagonal?
|
||
|
|
||
|
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.
|
||
|
"""
|
||
|
|
||
|
def triangle_generator(start = 0):
|
||
|
n = start
|
||
|
while True:
|
||
|
n = n + 1
|
||
|
yield n * (n + 1) / 2
|
||
|
def pentagonal_generator(start = 0):
|
||
|
n = start
|
||
|
while True:
|
||
|
n = n + 1
|
||
|
yield n * (3 * n - 1) / 2
|
||
|
def hexagonal_generator(start = 0):
|
||
|
n = start
|
||
|
while True:
|
||
|
n = n + 1
|
||
|
yield n * (2 * n - 1)
|
||
|
|
||
|
MIN = 40755
|
||
|
|
||
|
def main():
|
||
|
pent_count = 0
|
||
|
tri_count = 0
|
||
|
for hex in hexagonal_generator():
|
||
|
if hex <= MIN:
|
||
|
continue
|
||
|
# Catch up the other generators
|
||
|
for pent in pentagonal_generator(pent_count):
|
||
|
pent_count = pent_count + 1
|
||
|
if pent >= hex:
|
||
|
break
|
||
|
if pent > hex:
|
||
|
continue
|
||
|
for tri in triangle_generator(tri_count):
|
||
|
tri_count = tri_count + 1
|
||
|
if tri >= hex:
|
||
|
break
|
||
|
if tri > hex:
|
||
|
continue
|
||
|
print 'Next number is:', hex
|
||
|
break
|
||
|
if __name__ == '__main__':
|
||
|
main()
|