Skip to content

Instantly share code, notes, and snippets.

@OussaZaki
Created June 26, 2017 15:09
Show Gist options
  • Save OussaZaki/1f44e11305e88835bd6af8c3d7e7f1a2 to your computer and use it in GitHub Desktop.
Save OussaZaki/1f44e11305e88835bd6af8c3d7e7f1a2 to your computer and use it in GitHub Desktop.
Better implementation of Project Euler #2: Even Fibonacci numbers.
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