Skip to content

Commit 7b89d03

Browse files
melonbreadjinpoyea
authored andcommitted
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
1 parent 06dbef0 commit 7b89d03

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

project_euler/problem_02/sol4.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import math
2+
from decimal import *
3+
4+
getcontext().prec = 100
5+
phi = (Decimal(5) ** Decimal(0.5) + 1) / Decimal(2)
6+
7+
n = Decimal(int(input()) - 1)
8+
9+
index = (math.floor(math.log(n * (phi + 2), phi) - 1) // 3) * 3 + 2
10+
num = round(phi ** Decimal(index + 1)) / (phi + 2)
11+
sum = num // 2
12+
13+
print(int(sum))

0 commit comments

Comments
 (0)