Nodejs Fibonacci Sequence Get naiveFib()

Here you can find the source of naiveFib()

Method Source Code

/**/*w  w w  .  j av  a  2  s  . com*/
 * Returns the Nth fibonacci number.
 * @extends Number
 * @function naiveFib
 * @return {number}
 *
 * Examples:
 *     Number(10).naiveFib();
 *     => 55
 */
Number.prototype.naiveFib = function () {
  if (Number(this) < 0)
    return -1;
  switch (Number(this)) {
    case 0:
      return 0;
    case 1:
      return 1;
    default:
      return (this-1).naiveFib() + (this-2).naiveFib();
  }
}

Related

  1. iterFib()
    Number.prototype.iterFib = function () {
      var v1 = 0, v2 = 1, temp = 0;
      (new Array()).rangeArray(0, this-1).forEach(function(index) {
        temp = v2;
        v2 = v2 + v1;
        v1 = temp;
      });
      return v2;
    
  2. memoFib(hash=})
    Number.prototype.memoFib = function (hash={}) {
      if (Number(this) < 0) return -1;
      else if (Number(this) === 0) return 0;
      else if (Number(this) === 1) return 1;
      else {
        prevFib = hash[this-1] || (hash[this-1] = (this-1).memoFib(hash));
        prevPrevFib = hash[this-2] || (hash[this-2] = (this-2).memoFib(hash));
        return prevFib + prevPrevFib;
    
  3. sumFibs(num)
    Array.prototype.last = function() {
      return this[this.length-1];
    };
    Array.prototype.secondToLast = function() {
      return this[this.length-2];
    };
    function sumFibs(num) {
      var sequence = [1,1];
      while (sequence.secondToLast() + sequence.last() <= num) {
    ...