Skip to content

Instantly share code, notes, and snippets.

@martin-mok
Created January 12, 2020 00:12
Show Gist options
  • Select an option

  • Save martin-mok/e5299126c27c534f04d6dbb88a737b9d to your computer and use it in GitHub Desktop.

Select an option

Save martin-mok/e5299126c27c534f04d6dbb88a737b9d to your computer and use it in GitHub Desktop.
freecodecamp: Sum All Odd Fibonacci Numbers

Description

Given a positive integer num, return the sum of all odd Fibonacci numbers that are less than or equal to num. The first two numbers in the Fibonacci sequence are 1 and 1. Every additional number in the sequence is the sum of the two previous numbers. The first six numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8. For example, sumFibs(10) should return 10 because all odd Fibonacci numbers less than or equal to 10 are 1, 1, 3, and 5.

Tests

tests:
  - text: <code>sumFibs(1)</code> should return a number.
    testString: assert(typeof sumFibs(1) === "number");
  - text: <code>sumFibs(1000)</code> should return 1785.
    testString: assert(sumFibs(1000) === 1785);
  - text: <code>sumFibs(4000000)</code> should return 4613732.
    testString: assert(sumFibs(4000000) === 4613732);
  - text: <code>sumFibs(4)</code> should return 5.
    testString: assert(sumFibs(4) === 5);
  - text: <code>sumFibs(75024)</code> should return 60696.
    testString: assert(sumFibs(75024) === 60696);
  - text: <code>sumFibs(75025)</code> should return 135721.
    testString: assert(sumFibs(75025) === 135721);

Solution

Offical soln:

function sumFibs(num) {
  var a = 1;
  var b = 1;
  var s = 0;
  while (a <= num) {
    if (a % 2 !== 0) {
      s += a;
    }
    a = [b, b=b+a][0];
  }
  return s;
}

My soln:

function sumFibs(num) {
  let a=1,b=1,sum=1;
  while(b<=num){
    if(b%2==1) {
      sum+=b;
    }
    b=a+b;
    a=b-a;
  }
  return sum;
}

sumFibs(4);

forum soln:

function sumFibs(num) {
  // Perform checks for the validity of the input
  if (num < 0) return -1;
  if (num === 0 || num === 1) return 1;

  // Create an array of fib numbers till num
  const arrFib = [1, 1];
  let nextFib = 0;

  // We put the new Fibonacci numbers to the front so we
  // don't need to calculate the length of the array on each
  // iteration
  while ((nextFib = arrFib[0] + arrFib[1]) <= num) {
    arrFib.unshift(nextFib);
  }

  // We filter the array to get the odd numbers and reduce them to get their sum.
  return arrFib.filter(x => x % 2 != 0).reduce((a, b) => a + b);
}

If unshift was not used, we have to calculate the length of the array. As follow:
Sum All Odd Fibonacci Numbers

function sumFibs(num) {
  var findNextFib = function(a, b) {
    return a + b;
  };
  
  var fib = [1,1];
  var nextFib = findNextFib(1,1);
  
  while (nextFib <= num) {
    fib.push(nextFib);
    nextFib = findNextFib(fib[fib.length-2], fib[fib.length-1]);
  }
  
  return fib.filter(function(a) {
    return (a % 2 !== 0);
  })
  .reduce(function(a,b) {
    return a + b;
  });
}

Another way is use The formula for the sum of first N odd Fibonacci numbers:
a(n) = a(n-1) + 4a(n-2) – 4a(n-3) + a(n-4) – a(n-5) for n>5
Find the sum of first N odd Fibonacci numbers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment