diff --git a/src/Python/Problem045.py b/src/Python/Problem045.py new file mode 100644 index 0000000..4af0840 --- /dev/null +++ b/src/Python/Problem045.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +""" +Created on 09 Sep 2021 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for problem 45 of Project Euler +https://projecteuler.net/problem=45 +""" + +from utils import timeit + +def pentagonal(n): + return int(n*(3*n-1)/2) + +def hexagonal(n): + 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, ... + + 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)) + # all hexagonal numbers are also triangle numbers! + 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