0

Today i work on a problem which is:

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

This is a problem on Project Euler,here is the link:

https://projecteuler.net/problem=2

I Idon't know what's wrong with my code.I think my logic is right,but i still get a wrong answer. I have tested n<100,n<500,n<2000 and i get the right answer,so i think this code would be right when n<4000000.

Here is my code

#include <stdio.h>

long long int Fib(int n){

    if (n == 1){

        return 1;
    }
    else if (n == 2){

        return 2;
    }
    else {
        return (Fib(n - 1) + Fib(n - 2));
    }
}

int main()
{
    int i;

    long long int sum=0;

    for(i=2;Fib(i)<4000000;i=i+2){

        sum+= Fib(i);
    }
    printf("%lld",sum);

    return 0;
}

when n<4000000,my answer is 5702886

  • 4
    You should be summing even-valued terms, but it looks like you're summing every other term instead. – frslm Jul 20 '18 at 17:23
  • 1
    Keep it simple. Recursion is over-kill and `long long` is unnecessary. – Weather Vane Jul 20 '18 at 17:30
  • a very simple problem. step 1) only keep the currently calculated value and the prior calculated value. 2) calculate the latest value 3) as each value is calculated, check if it is 'even' 5) if 'even' then add to sum 4) move currently calculated value to prior calculated value, loop to 2 – user3629249 Jul 22 '18 at 01:14
  • an array of 4million will be a problem when placed on the stack in a windows environment (where the max stack is only 4k – user3629249 Jul 22 '18 at 01:16

2 Answers2

0

Firstly, in your question you say "whose values do not exceed four million", since 4M doesn't exceed 4M it should be "<=4M". But I don't think that's your mistake, the big mistake is that in the main function, in the for, you are jumping in steps of "i = i+2" when what you actually want to do is sum the even elements of the fibonacci sequence. Not the even index's in the list of elements.

Solution: the easy solution here ( very inefficient ) is to change the step on the for to "i++" and do an if inside:

 if(Fib(i) % 2 == 0)
     sum += Fib(i);

Extra: If you want to fix your code to be efficient, there are some things you can do. If you notice when you call fib(i+x) you will calculate again several iterations that you had already done before. This is easily fix by having a vector which saves each element of the fibonacci sequence and then at the end just sum the ones that are even.

EDIT: here is an example alternative (more efficient) solution: Use with caution: this was not tested can have small mistakes.

    int main(){

    int fibbs[4000000]; // it will never be bigger than this
    int sum = 0;
    int numValues = 0; // counts the number of values in the vector

   // initialize vector to 0's , probably not necessary
    for(i = 0; i < 4000000; i++){
        fibs[i] = 0;
    }

    // first positions of fibonacci
    fibs[0] = 1;
    fibs[1] = 1;
    fibs[2] = 2;



    // fill the vector with all the fibonacci numbers until 4M
    int i = 3;
    while(1){
        fibs[i] = fibs[i-1] + fibs[i-2];
        if(fibs[i] > 4000000){
            fibs[i] = 0; // it is already bigger than 4M , can't sum this one
            numValues = i-1; // save the number of values in the vector
            break;
        }
        i++;
    }

    for(i = 0; i <= numValues; i++){
        if(fibs[i] % 2 == 0 ){
            sum += fibs[i];
        }
    }

    printf("Sum is: %d\n", sum);
    return 0;

    }
Miguel
  • 1,295
  • 12
  • 25
  • As an example of your mistake, Fib(5) = 8 and you are not adding it. I don't know how did you assume you got the other values correct. – Miguel Jul 20 '18 at 18:29
  • 1
    You set the first positions and then set the entire array to `0`. Maybe remove that loop and just say `int fibs[4000000] = {0};`, didn't really look at all of your code btw – Tormund Giantsbane Jul 20 '18 at 20:55
  • 1
    I do know where my mistake is. The problem wants a sum of Fib(i) % 2 ==0, – doodle daisy Jul 21 '18 at 05:55
0

I do know where my mistake is. The problem wants a sum of Fib(i) % 2 ==0,but my code is trying to find out the sum of 2th 4th 6th...I make a wrong understang of the problem.