From 7b89d03dd7d80087fa95bcf7a1983fe3d8b424ca Mon Sep 17 00:00:00 2001 From: yolstatrisch Date: Thu, 2 May 2019 00:44:21 +0800 Subject: [PATCH] Added an O(1) solution to problem 002 (#776) * Added an O(1) solution to problem 002 * Removed comments from sol3.py that were accidentally added to sol4.py --- project_euler/problem_02/sol4.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 project_euler/problem_02/sol4.py diff --git a/project_euler/problem_02/sol4.py b/project_euler/problem_02/sol4.py new file mode 100644 index 000000000..64bae65f4 --- /dev/null +++ b/project_euler/problem_02/sol4.py @@ -0,0 +1,13 @@ +import math +from decimal import * + +getcontext().prec = 100 +phi = (Decimal(5) ** Decimal(0.5) + 1) / Decimal(2) + +n = Decimal(int(input()) - 1) + +index = (math.floor(math.log(n * (phi + 2), phi) - 1) // 3) * 3 + 2 +num = round(phi ** Decimal(index + 1)) / (phi + 2) +sum = num // 2 + +print(int(sum))