How to check fibonacci number in python

 21 - Feb - 2022
 XpertPhp
 24

In this example, We will explain to you how to check fibonacci number in python. so you can see our fibonacci number example. Example

import math
def isPerfectSquare(x):
    s = int(math.sqrt(x))
    return s*s == x
def isFibonacci(n):
    return isPerfectSquare(5*n*n + 4) or isPerfectSquare(5*n*n - 4)
for i in range(1,5):
     if (isFibonacci(i) == True):
         print (i,"is a Fibonacci Number")
     else:
         print (i,"is a not Fibonacci Number ")
Output
1 is a Fibonacci Number
2 is a Fibonacci Number
3 is a Fibonacci Number
4 is a not Fibonacci Number