Project Euler #2: Even Fibonacci numbers

Sort by

recency

|

636 Discussions

|

  • + 0 comments

    Haskell

    import Control.Applicative
    import Control.Monad
    import System.IO
    
    all_fibs = 1 : 2 : zipWith (+) fibs (tail fibs)
    fibs =  take 100 all_fibs 
    
    reduce :: [Int] -> [Int]
    reduce xs = filter (\n -> n `mod` 2 == 0) xs
    
    cutoff :: Int -> [Int] -> [Int]
    cutoff n xs = filter (\m -> m <= n && m > 0) xs
                     
    main :: IO()
    main = do
        t_temp <- getLine
        let t = read t_temp :: Int
        forM_ [1..t] $ \a0  -> do
            n_temp <- getLine
            let n = read n_temp :: Int
            
            let cutoff_fibs = cutoff n fibs  
            let even_fibs = reduce cutoff_fibs
            
            let fibSum = sum even_fibs :: Int
            putStrLn (show fibSum)
            
    
  • + 0 comments

    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]

  • + 0 comments

    import java.util.Scanner;

    public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
    
        while (t-- > 0) {
            long n = in.nextLong();
            long a = 1, b = 2, sum = 0;
    
    
            while (b <= n) {
                if (b % 2 == 0) {
                    sum += b;
                }
    
    
                long temp = a + b;
                a = b;
                b = temp;
            }
    
    
            System.out.println(sum);
        }
    
        in.close();
    } 
    

    }

  • + 0 comments
    defining a function to check the given condition

    def sum_fib(n): a,b= 1,2 even_sum = 0 while b<=n: if b%2==0: even_sum +=b a,b=b,a+b return even_sum

    if name == 'main': t = int(input().strip())

    for t_itr in range(t):
        n = int(input().strip())
        res = sum_fib(n)
        print(res)
    
  • + 0 comments
    #!/bin/python3
    
    import sys
    
    t = int(input().strip())
    for a0 in range(t):
        n = int(input().strip())
        def fibonaci(n):
            if n == 1:
                return 1
            if n <= 0:
                return 0
            a, b = 0, 1  # Defining the 1st and 2nd position values of the Fibonacci series
            ans = 0
            while True:  # Creating a loop to update the values of a and b so that we can check if the next value is even or not
                if a % 2 == 0:  # Considering a to always be the first value and checking if it is even or not
                    ans += a  # If the value stored in a is even, add it to the result
                a, b = b, a + b  # Swap the values: set a to b and b to the sum of a and b as per the Fibonacci series (e.g., 0 1 1 2 3 5 8 -> a=0 b=1 -> step 2 a=1 b=1 -> step 3 a=1 b=2 and so on)
                if a > n:  # Check if the value of a exceeds the given n, and if so, break the loop
                    break
            print(ans)  # Output the final answer
        fibonaci(n)
    

    For the sake of discussion and explanation of the question, I am providing detailed comments within the code. Please refer to these comments for a clear understanding of the logic and functionality of the code.