mirror of
https://github.com/correl/euler.git
synced 2024-11-23 19:19:53 +00:00
Problem 045
git-svn-id: file:///srv/svn/euler@71 e5f4c3ec-3c0c-11df-b522-21efaa4426b5
This commit is contained in:
parent
29cdc8556a
commit
b56f368d3c
1 changed files with 54 additions and 0 deletions
54
e045.py
Normal file
54
e045.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
"""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()
|
Loading…
Reference in a new issue