# How to find-out if given number is Fibannaci number?

326 views
How to find-out if given number is Fibannaci number?
posted Nov 28, 2013

Check the following program based on property n is Fibinacci if one of 5*n*n + 4 or 5*n*n - 4 or both is a perfect square

``````bool isPerfectSquare(int x)
{
int s = sqrt(x);
return (s*s == x);
}

bool isFibonacci(int n)
{
return isPerfectSquare(5*n*n + 4) ||
isPerfectSquare(5*n*n - 4);
}
``````
+1 vote

A simple way is to generate Fibonacci numbers until the generated number is greater than or equal to ‘n’.

However a other property of this number can be used is "A number is Fibonacci if and only if one or both of (5*n^2 + 4) or (5*n^2 – 4) is a perfect square (Source: Wiki)."

Similar Questions

Help me to write a C program which can generate list of numbers which are palindrome in decimal and octal notion. You can take some max as a limit find out all numbers which satisfy the given property between 1 to n.

Say the given string is `ABC`
Output should be `ABC` `ACB` `BAC` `BCA` `CBA` `CAB`