From 731aecfad396c30f392c758a1bd5f9b1fc5f9367 Mon Sep 17 00:00:00 2001 From: Philippe Zwietering Date: Tue, 2 Mar 2021 00:03:01 +0100 Subject: [PATCH] 45 really easy once you can quickly verify pentagonal numbers --- 45/main.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 45/main.py diff --git a/45/main.py b/45/main.py new file mode 100644 index 0000000..1eec484 --- /dev/null +++ b/45/main.py @@ -0,0 +1,35 @@ +''' +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. +''' + +# Observe that every other triangle number is also a hexagonal number, so the 1st, the 3rd, 5th, etc. +# So we only need to check all the uneven n triangle numbers to be pentagonal + +# In addition, we can check if a number is pentagonal by checking if sqrt(1 + 24 n) % 6 == 5 + +import math + +def t(n): + return n * (n+1) / 2 + +def isPentagonal(x): + return math.sqrt(1 + 24 * x) % 6 == 5 + +def main(): + print("Hello this is Patrick") + + n = 287 + while not isPentagonal(t(n)): + n += 2 + + print(t(n)) + +if __name__ == "__main__": + main()