Problem 2: Even Fibonacci Numbers I didn't understand this challenge

I’ve looked at the challenge after your post, and certainly this challenge has some quirks in it.

Basically to get the correct output given by each test case, you need to take away the first 2 sequences from typical Fibonacci sequence.

Consider the first ten Fibonacci sequence (that starts from 1)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
Here, the even sum is 2 + 8 + 34 = 44. But this is far from the given test value, which demands 188.

Now, consider the first twelve Fibonacci sequence
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
Here, the even sum is 2 + 8 + 34 + 144 = 188
So, feboEvenSum(10) actually requires the first twelve sequence.

Does this pattern continue with the other test cases? Yes.

Try it out in an online editor that supports ES6 generator. e.g) repl.it, JSFiddle

function *fib() {
  let a = 0, b = 1
    
  for(let tmp; ;) {
    yield b

    tmp = a
    a = b
    b += tmp
  }
}

let seq = (gen) => (n) => {
    let g = gen(), res = []
    while(--n >= 0) res.push(g.next().value)
    return res
}


let fibseq = seq(fib)
let even = (n) => n % 2 === 0
let sum = (a, b) => a + b

let sumEvenFib = (n) => fibseq(n).filter(even).reduce(sum)

console.log(sumEvenFib(10))
console.log(sumEvenFib(11))
console.log(sumEvenFib(12))

console.log(sumEvenFib(23))
console.log(sumEvenFib(24))
console.log(sumEvenFib(25))

console.log(sumEvenFib(43))
console.log(sumEvenFib(44))
console.log(sumEvenFib(45))

I wrote the code such that OP won’t likely to understand to avoid spoiling the direct solution.

The conclusion is that the challenge is not well described and has weird Fibonacci sequence definition.

1 Like

I know , that is from first output

<code>function fiboEvenSum(number) {
    // You can do it!

    var fibbArr =[];
    var curr =1;
  var back =0;
      for (var x=0;x<=  number; x++){  
          if (fibbArr.length<2){
            
              } else{curr = fibbArr[x-1] ; back=fibbArr[x-2];}
          var currentNumber =curr+back;
      //if(currentNumber<4000000)    
fibbArr.push((currentNumber));
     
    
      }
  console.log(fibbArr.toString());
 var out= fibbArr.filter((d)=> d%2!==0);

 
   return out.reduce((a,b)=>a+b);
 
  }
  
  console.log(fiboEvenSum(31));</code>

Hi all,

I went ahead and opened an issue on the FCC GitHub

I’m arguing that the tests are off by one. If you wanted to be a purist and go with a Fibonacci series that included two #1 then I guess it would be off by a count of two.

Anyone can chime in here or on GitHub if they see things differently. If anyone feels strongly about a 1,1, 2, 3, 5, 8… series vs 1, 2, 3, 5, 8… the developers would probably appreciate hearing that sooner rather than later.

See also my post above Problem 2: Even Fibonacci Numbers I didn't understand this challenge

It’s that the challenge was poorly designed and Project Euler original makers were so lazy to say that the sequence should start from 0 and not from 1.

By the way, I have solved it yesterday after 4 days trying to understand what the challenge says at all, at the end of they day I discovered that the sequence’s first element should start from 0 and not from actual 1. Just to make it clear for anyone who found the same problem.

While checking, the system counts the sum up to 13 position of Fibonaci due to it start from 3rd position and get the result 188 instead of 44.
Same with sum of even of first 43 figures, the required response is in sum of 44 figures but not 43

I think the unlucky intention of the exercise should be understood like this:

0: 1
1: 2
2: 3
3: 5
4: 8
5: 13
6: 21
7: 34
8: 55
9: 89
10: 144

Sum 10 first even values
2 + 8 + 34 + 144 = 188