diff --git a/src/Python/Problems001-050/Problem045.py b/src/Python/Problems001-050/Problem045.py index 4af0840..4c396e0 100644 --- a/src/Python/Problems001-050/Problem045.py +++ b/src/Python/Problems001-050/Problem045.py @@ -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(3n−1)/2 1, 5, 12, 22, 35, ... - Hexagonal Hn=n(2n−1) 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()}") \ No newline at end of file +if __name__ == "__main__": + print(f"Result for Problem 45 is {compute()}")