diff --git a/Fibonacci-number-detector-function/function.py b/Fibonacci-number-detector-function/function.py index 2c9b684..4004d7d 100644 --- a/Fibonacci-number-detector-function/function.py +++ b/Fibonacci-number-detector-function/function.py @@ -1,10 +1,11 @@ +import math + +# A utility function that returns true if x is perfect square +def isPerfectSquare(x): + s = int(math.sqrt(x)) + return s*s == x + +# Returns true if n is a Fibonacci Number, else false 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: - return False + # n is Fibonacci iff (5*n*n + 4 or 5*n*n - 4) is a perfect square + return isPerfectSquare(5*n*n + 4) or isPerfectSquare(5*n*n - 4) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy