Created
June 26, 2017 15:09
-
-
Save OussaZaki/1f44e11305e88835bd6af8c3d7e7f1a2 to your computer and use it in GitHub Desktop.
Better implementation of Project Euler #2: Even Fibonacci numbers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def even_fibonacci_sum(n): | |
fn_2 = 2 #Fn-2 | |
fn_1 = 8 #Fn-1 | |
sum = 10 #first even number Fn-2 + Fn-1 | |
while True : | |
fn = 4 * fn_1 + fn_2 | |
if fn >= n: return sum | |
sum += fn | |
fn_2, fn_1 = fn_1, fn | |
t = int(input().strip()) | |
for i in range(t): | |
n = int(input().strip()) | |
print(even_fibonacci_sum(n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment