Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #program to chack if a number is a fibonacci number or not
- import math
- # method that checks if a number is a perfect square or not
- def isPerfectSquare(num):
- n = int(math.sqrt(num))
- if(n*n == num):
- return True
- else:
- return False
- # method to check if a number is a fibonacci number or not
- def isFibonacci(num):
- # calculating numbers to check if they are perfect squares or not
- temp1 = 5*num*num + 4
- temp2 = 5*num*num - 4
- if(isPerfectSquare(temp1) or isPerfectSquare(temp2)):
- return True
- else:
- return False
- # main function
- myList = [22, 15, 5, 8, 21, 34, 38, 75, 55, 89]
- for i in myList:
- fibo = isFibonacci(i)
- if(fibo):
- print(str(i) + " is a Fibonacci number.")
- else:
- print(str(i) + " is not a Fibonacci number.")
Add Comment
Please, Sign In to add comment