Adopted new convention from template

This commit is contained in:
David Doblas Jiménez 2022-10-02 18:37:24 +02:00
parent e74454e4f5
commit 037fb1de95

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3
#!/usr/bin/env python
"""
Created on 09 Sep 2021
@ -11,32 +11,36 @@ https://projecteuler.net/problem=45
from utils import timeit
def pentagonal(n):
return int(n*(3*n-1)/2)
return int(n * (3 * n - 1) / 2)
def hexagonal(n):
return int(n*(2*n-1))
return int(n * (2 * n - 1))
@timeit("Problem 45")
def compute():
"""
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(3n1)/2 1, 5, 12, 22, 35, ...
Hexagonal Hn=n(2n1) 1, 6, 15, 28, 45, ...
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.
"""
pentagonal_list = set(pentagonal(n) for n in range(2,100_000))
pentagonal_list = set(pentagonal(n) for n in range(2, 100_000))
# all hexagonal numbers are also triangle numbers!
hexagonal_list = set(hexagonal(n) for n in range(2,100_000))
hexagonal_list = set(hexagonal(n) for n in range(2, 100_000))
ans = sorted(hexagonal_list & pentagonal_list)
# First one is already known
return ans[1]
if __name__ == "__main__":
print(f"Result for Problem 45: {compute()}")
if __name__ == "__main__":
print(f"Result for Problem 45 is {compute()}")