Daniele Fucini 3503357703
Add comments
Added comments to all the python solutions implemented so far.
2019-09-26 15:57:04 +02:00

43 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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(3n1)/2 1, 5, 12, 22, 35, ...
# Hexagonal H_n=n(2n1) 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()