m2skills

fibocheck python

May 31st, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. #program to chack if a number is a fibonacci number or not
  2. import math
  3.  
  4. # method that checks if a number is a perfect square or not
  5. def isPerfectSquare(num):
  6.     n = int(math.sqrt(num))
  7.     if(n*n == num):
  8.         return True
  9.     else:
  10.         return False
  11.  
  12. # method to check if a number is a fibonacci number or not     
  13. def isFibonacci(num):
  14.    
  15.     # calculating numbers to check if they are perfect squares or not
  16.     temp1 = 5*num*num + 4
  17.     temp2 = 5*num*num - 4
  18.    
  19.     if(isPerfectSquare(temp1) or isPerfectSquare(temp2)):
  20.         return True
  21.     else:
  22.         return False
  23.        
  24.  
  25. # main function
  26. myList = [22, 15, 5, 8, 21, 34, 38, 75, 55, 89]
  27. for i in myList:
  28.     fibo = isFibonacci(i)
  29.     if(fibo):
  30.         print(str(i) + " is a Fibonacci number.")
  31.     else:  
  32.         print(str(i) + " is not a Fibonacci number.")
Add Comment
Please, Sign In to add comment