From 5b67c2610f0cca247cf9c619c49df7378e749681 Mon Sep 17 00:00:00 2001 From: ObaidaHY Date: Sat, 18 Jun 2022 20:48:12 +0300 Subject: [PATCH] changing the implementation of is_Fibonacci function --- .../function.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) 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