Project Euler #2: Even Fibonacci numbers

  • + 0 comments

    well this won't give any timeouts but one test case is failing , i think it's because of inaccuracy we get in golden ratio while calculating it for very large term for e.g. the following code will generate 27th term as 37889062373143904 instead of 37889062373143906. rectify it and you will get the correct code :)

    int main(){
        int t; 
        scanf("%d",&t);
        for(int a0 = 0; a0 < t; a0++){
            long n, sum = 0; 
            scanf("%ld",&n);
            long curr_num = 2;
            while(curr_num<=n){
                sum += curr_num;
                curr_num = (long)floor((pow(((1 + pow(5 , 0.5)) / 2),3) * curr_num + 0.5));
                //sum += curr_num;
            }
            printf("%ld \n", sum);
        }
        return 0;
    }