From ead9f6e82d865147bfc7ac2a90e86f8931afb011 Mon Sep 17 00:00:00 2001 From: Philippe Zwietering Date: Mon, 1 Mar 2021 23:45:01 +0100 Subject: [PATCH] Not even that slow of a solution for 44 --- 44/main.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 44/main.py diff --git a/44/main.py b/44/main.py new file mode 100644 index 0000000..6156eec --- /dev/null +++ b/44/main.py @@ -0,0 +1,48 @@ +''' +Pentagonal numbers are generated by the formula, Pn=n(3nāˆ’1)/2. The first ten pentagonal numbers are: + +1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... + +It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, 70 āˆ’ 22 = 48, is not pentagonal. + +Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference are pentagonal and D = |Pk āˆ’ Pj| is minimised; what is the value of D? +''' + +import math + +def p(n): + return int(n * (3 * n - 1) / 2) + +def isTriangle(n, triangleSet, triangleList): + if n > triangleList[-1]: + toBeat = triangleList[-1] + m = len(triangleList) + + while toBeat < n: + m += 1 + toBeat = p(m) + triangleList.append(toBeat) + triangleSet.add(toBeat) + + return n == triangleList[-1] + + else: + return n in triangleSet + +def main(): + print("Hello this is Patrick") + + bestDif = math.inf + triangleList = [1, 5] + triangleSet = set(triangleList) + + for i in range(2, 10000): + for j in range(1, i): + if isTriangle(p(i) - p(j), triangleSet, triangleList) and isTriangle(p(i) + p(j), triangleSet, triangleList): + bestDif = min(bestDif, abs(p(i) - p(j))) + + print(bestDif) + + +if __name__ == "__main__": + main() \ No newline at end of file