Python Program to check if a number is a Fibonacci number?


Using the function

In [1]:
n = int(input("Please enter a positive number : "))

def is_Perfect_Square(n):
    s = int(n ** 0.5)
    return s*s == n

if (is_Perfect_Square(5*n*n+4) or is_Perfect_Square(5*n*n-4)):
    print(n, "is fibanacci.")
else:
    print(n, "is not fibanacci.")
Please enter a positive number : 4
4 is not fibanacci.

In this program, we ask the user to enter the number and check if (5*n*n+4) or (5*n*n-4) or both are the perfect squares. If any of these is the perfect square, then the number is a Fibonacci number.