43 lines
1.0 KiB
Python
43 lines
1.0 KiB
Python
#!/usr/bin/python3
|
||
|
||
# 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 math import sqrt
|
||
|
||
from timeit import default_timer
|
||
from projecteuler import is_pentagonal
|
||
|
||
def main():
|
||
start = default_timer()
|
||
|
||
found = 0
|
||
i = 143
|
||
|
||
while not found:
|
||
i = i + 1
|
||
# Hexagonal numbers are also triangle numbers, so it's sufficient
|
||
# to generate hexagonal numbers (starting from H_144) and check if
|
||
# they're also pentagonal.
|
||
n = i * (2 * i - 1)
|
||
|
||
if is_pentagonal(n):
|
||
found = 1
|
||
|
||
end = default_timer()
|
||
|
||
print('Project Euler, Problem 45')
|
||
print('Answer: {}'.format(n))
|
||
|
||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||
|
||
if __name__ == '__main__':
|
||
main()
|