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 Created on 09 Sep 2021
@ -11,32 +11,36 @@ https://projecteuler.net/problem=45
from utils import timeit from utils import timeit
def pentagonal(n): def pentagonal(n):
return int(n*(3*n-1)/2) return int(n * (3 * n - 1) / 2)
def hexagonal(n): def hexagonal(n):
return int(n*(2*n-1)) return int(n * (2 * n - 1))
@timeit("Problem 45") @timeit("Problem 45")
def compute(): def compute():
""" """
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
Triangle Tn=n(n+1)/2 1, 3, 6, 10, 15, ... Triangle Tn=n(n+1)/2 1, 3, 6, 10, 15, ...
Pentagonal Pn=n(3n1)/2 1, 5, 12, 22, 35, ... Pentagonal Pn=n(3n-1)/2 1, 5, 12, 22, 35, ...
Hexagonal Hn=n(2n1) 1, 6, 15, 28, 45, ... Hexagonal Hn=n(2n-1) 1, 6, 15, 28, 45, ...
It can be verified that T285 = P165 = H143 = 40755. It can be verified that T285 = P165 = H143 = 40755.
Find the next triangle number that is also pentagonal and hexagonal. 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! # 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) ans = sorted(hexagonal_list & pentagonal_list)
# First one is already known # First one is already known
return ans[1] return ans[1]
if __name__ == "__main__":
print(f"Result for Problem 45: {compute()}") if __name__ == "__main__":
print(f"Result for Problem 45 is {compute()}")