Project Euler #2: Even Fibonacci numbers

Sort by

recency

|

624 Discussions

|

  • + 0 comments

    couple hints: 1. don't compute all fibonacci numbers, the even ones are sufficient 2. store a fine selection of even fibonacci numbers and their pre sums for later reference 3. you can even speed up the search for the matching even fibonacci number by using a matching exponential factor and some log functions

  • + 0 comments

    import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;

    public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
       for(int a0 = 0; a0 < t; a0++){
            long n = in.nextLong();  // Read the limit N for this test case
            long sum = 0;
    
            // Starting the Fibonacci sequence
            long a = 1, b = 2;
    
            // Calculate the sum of even Fibonacci numbers not exceeding N
            while (b <= n) {
                if (b % 2 == 0) {
                    sum += b;
                }
    
                // Generate the next Fibonacci number
                long next = a + b;
                a = b;
                b = next;
            }
    
            // Output the result for this test case
            System.out.println(sum);
        }
    
        in.close(); 
    }
    

    }

  • + 0 comments

    Easier than #1

  • + 0 comments

    Python 3 solution. Thoughts?

    import sys
    
    t = int(input().strip())
    
    for a0 in range(t):
        n = int(input().strip())
        
        a, b, s = 1, 2, 0
        
        while b <= n:
            if b % 2 == 0:
                s += b
                
            a, b = b, a + b
            
        print(s)
    
  • + 1 comment

    public static long fibonacci(long count){ long i= 0, k = 0, j= 1; long sum = 0; while(i

        }
        return sum;
    }