Project Euler #2: Even Fibonacci numbers

Sort by

recency

|

634 Discussions

|

  • + 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.

  • + 0 comments

    **//1,1,2,3,5,8,13,21,34,55,89,144 //using the knowledge that after every two terms comes an even term //Sum of two odds is an even(two oods then an even) ** int main(){ int t; cin >> t; for(int a0 = 0; a0 < t; a0++){ long n; cin >> n; long a1=1; long a2=1;

        long sum=a1+a2;
        while(a1+2*a2<n){
            long x=2*a2+a1;
            a2=x+a1+a2;
            a1=x;
            sum+=a1+a2;
    
        }
                //if the current term was greater than n, subtract their sum from the overall sum
        if(a1+a2>n){
            sum-=(a1+a2);
        }
        cout<<sum<<endl;
    }
    return 0;
    

    }

  • + 0 comments

    KOTLIN Code

    fun main(args: Array<String>) {
        val t = readLine()!!.trim().toInt()
    
        for (tItr in 1..t) {
            val n = readLine()!!.trim().toLong()
            createFibonacciSum(n)
        } 
    }
    
    private fun createFibonacciSum(n: Long){
        var left = 0L
        var middle = 1L
        var sum = 0L
        var i = 0
        while(i< n && (middle + left)< n){
            var right = middle + left
            left = middle
            middle = right
            if(right % 2L == 0L) sum += right
            i++ 
        }
        println(sum)
    }