3

Is there an efficient way to check if number belongs to Fibonacci sequence?

I've seen many examples with a loop that creates the sequence in an array and checks every time if newly generated number of the sequence is equal to the input number. Is there another way?

1
  • You could do an exponential search for an upper bound and then take f0 as a lower bound and do a binary search for a valid fibonacci number. I am unsure if it is possible any faster, ask a mathematician. Commented Nov 16, 2016 at 18:28

5 Answers 5

7

http://www.geeksforgeeks.org/check-number-fibonacci-number/

This link details that there is a special quality about fibonacci numbers that means that a number is Fibonacci if and only if one or both of (5*n2 + 4) or (5*n2 – 4) is a perfect square.

So,

function (num) {
    if (isSquare(5*(num*num)-4) || isSquare(5*(num*num)+4)) {
       return true;
    } else { return false; }
}

Then isSquare would just be a simple checking function.

Edit: Worth noting that while this is a much more efficient and easy way to find fibonacci numbers, it does have an upper bound. At about the 70th Fibonacci number and above, you may see issues because the numbers are too large.

Sign up to request clarification or add additional context in comments.

Comments

2
function isFibonacci(num, a = 0, b = 1) {
  if(num === 0 || num === 1) {
    return true;
  }

  let nextNumber = a+b;

  if(nextNumber === num) {
    return true;
  }
  else if(nextNumber > num) {
    return false;
  }

 return isFibonacci(num, b, nextNumber);
}

Comments

0
function isPerfectSquare(n) {
    return n > 0 && Math.sqrt(n) % 1 === 0;
};

//Equation modified from http://www.geeksforgeeks.org/check-number-fibonacci-number/
function isFibonacci(numberToCheck)
{
    // numberToCheck is Fibinacci if one of 5*n*n + 4 or 5*n*n - 4 or both
    // is a perferct square
    return isPerfectSquare(5*numberToCheck*numberToCheck + 4) ||
           isPerfectSquare(5*numberToCheck*numberToCheck - 4);
}


for(var i = 0; i<= 10000; ++i) {
    console.log(i + " - " + isFibonacci(i));
}

This will most likely fail for larger numbers though.

Comments

-1
def is_squared(number):
   temp_root = math.sqrt(number);
   temp_root = int(temp_root);
   return (temp_root * temp_root == number);

def check_all_fibo(test_number_list):
    result_fibo_list = [];
    for item in test_number_list:
        if item==0 or item == 1 or item == 2:
            result_fibo_list.append(item);
            continue;
        if is_squared(5 * item * item - 4) or is_squared(5 * item * item + 4):
            result_fibo_list.append(item);
    return result_fibo_list;

this is a python implementation by me. But keep in mind, the formula only works when the fib is not too large.

Comments

-1

The Fibonacci sequence is a series of numbers where a number is the addition of the last two numbers, starting with 0, and 1. Th following js function is explaining this.

    function isFabonacci(n) {
        if (n === 1 || n === 0) {
            return true;
        }
        let firstPrevNumber = n - 1;
        let secondPrevNumber = n - 2;
        return (firstPrevNumber + secondPrevNumber === n);
    }
    // isFabonacci(2) -> false
    // isFabonacci(3) -> true

1 Comment

This doesn't work for any number past 3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.