Skip to content

Commit 5b67c26

Browse files
committed
changing the implementation of is_Fibonacci function
1 parent 64c5bc1 commit 5b67c26

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
19
def isFibonacci(n):
2-
s = []
3-
a, b = 0, 1
4-
while a <= n:
5-
s = [a]
6-
a, b = b, a + b
7-
if n in s:
8-
return True
9-
else:
10-
return False
10+
# 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

Comments
 (0)