Project Euler #2: Even Fibonacci numbers

  • + 7 comments

    Hi dude, I have implemented the idea you have said in C++. I am getting 2nd 3rd cases time limit exceeded. Could you suggest me where I am going wrong. https://ideone.com/0shXEY

    • + 3 comments

      I've just copied my code from C++ and changed it to Java (used long instead of long long int) and it works like a charm!

      • + 0 comments

        Same worked for me! thanks

      • + 1 comment

        It's working for me also, But what's the logic behind this?

        • [deleted]
          + 1 comment

          Most easy solution in Java & it passes all the test cases:

          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();
                  long first = 0, second = 1, sum =0, sumeven =0;
          
                  while(second <= n){
                      sum = first+second;
                      first = second;
                      second = sum; 
                      if(sum%2==0 && sum<n){
                          sumeven += sum;
                      }
                  }
                  System.out.println(sumeven);
              }
          }
          

          }

      • + 0 comments

        https://www.hackerrank.com/contests/projecteuler/challenges/euler002/forum/comments/1109726

    • + 0 comments

      every variable and function use long long and you could get it.

    • + 0 comments

      use unsigned long long int sum=0; long int f0 = 2; long int f1 = 8; long int f2

    • + 0 comments

      same dude, I solved using matrix exponentiation but still got WA

    • + 0 comments

      Change the parameter to long n, and its all right

    • + 0 comments

      You just gotta use long long and you'll get it. Don't know why all these people are downvoting you instead of just pointing it out.