We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 64c5bc1 commit 5b67c26Copy full SHA for 5b67c26
Fibonacci-number-detector-function/function.py
@@ -1,10 +1,11 @@
1
+import math
2
+
3
+# A utility function that returns true if x is perfect square
4
+def isPerfectSquare(x):
5
+ s = int(math.sqrt(x))
6
+ return s*s == x
7
8
+# Returns true if n is a Fibonacci Number, else false
9
def isFibonacci(n):
- s = []
- a, b = 0, 1
- while a <= n:
- s = [a]
- a, b = b, a + b
- if n in s:
- return True
- else:
10
- return False
+ # n is Fibonacci iff (5*n*n + 4 or 5*n*n - 4) is a perfect square
11
+ return isPerfectSquare(5*n*n + 4) or isPerfectSquare(5*n*n - 4)
0 commit comments