We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Project Euler #2: Even Fibonacci numbers
Project Euler #2: Even Fibonacci numbers
Sort by
recency
|
639 Discussions
|
Please Login in order to post a comment
import math import os import random import re import sys
def even_fib_sum(n): a, b = 1, 2 total = 0 while b <= n: if b % 2 == 0: total += b a, b = b, a + b return total
if name == 'main': t = int(input().strip()) for t_itr in range(t): n = int(input().strip()) print(even_fib_sum(n))
def fibnacci(n): total = 0 f0, f1 = 0, 1 while f0 <= n: if f0 % 2 == 0: total += f0 f0, f1 = f1, f0 + f1 return total
visit site
Haskell
lst=[1,2] t = int(input().strip()) for a0 in range(t): n = int(input().strip()) for i in range(2,n): nextt=lst[i-1]+lst[i-2] if nextt>n: break else: lst.append(nextt) e=[j for j in lst if j%2==0] print(sum(e)) lst=[1,2]