import math
def isPerfectSquare(x):
s = int(math.sqrt(x))
return s*s == x
def isFibonacci(n):
# n is Fibonacci if one of 5*n*n + 4 or 5*n*n - 4 or both
# is a perfect square
return isPerfectSquare(5*n*n + 4) or isPerfectSquare(5*n*n - 4)
for i in range(1, 11):
if (isFibonacci(i) == True):
print(i, "is a Fibonacci Number")
else:
print(i, "is a not Fibonacci Number ")
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter