Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program for How to check if a given number is a Fibonacci number?
In this article, we will learn how to check if a given number is a Fibonacci number using a mathematical property rather than generating the entire Fibonacci sequence.
Problem Statement
Given a number n, we need to determine whether n is a Fibonacci number or not.
The Fibonacci sequence starts with 0 and 1, where each subsequent number is the sum of the previous two numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Mathematical Property
A number is Fibonacci if and only if (5*n² + 4) or (5*n² - 4) is a perfect square. This mathematical property allows us to check Fibonacci numbers efficiently without generating the sequence.
Implementation
Helper Function to Check Perfect Square
First, we create a function to check if a number is a perfect square ?
import math
def isPerfectSquare(x):
s = int(math.sqrt(x))
return s * s == x
# Test the function
print(isPerfectSquare(25)) # True
print(isPerfectSquare(26)) # False
True False
Main Function to Check Fibonacci Number
Now we implement the main function that uses the mathematical property ?
import math
def isPerfectSquare(x):
s = int(math.sqrt(x))
return s * s == x
def isFibonacci(n):
# Check if either (5*n² + 4) or (5*n² - 4) is a perfect square
return isPerfectSquare(5*n*n + 4) or isPerfectSquare(5*n*n - 4)
# Test with some numbers
test_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for num in test_numbers:
if isFibonacci(num):
print(f"{num} is a Fibonacci Number")
else:
print(f"{num} is not a Fibonacci Number")
1 is a Fibonacci Number 2 is a Fibonacci Number 3 is a Fibonacci Number 4 is not a Fibonacci Number 5 is a Fibonacci Number 6 is not a Fibonacci Number 7 is not a Fibonacci Number 8 is a Fibonacci Number 9 is not a Fibonacci Number 10 is not a Fibonacci Number
How It Works
The algorithm works by:
- Calculating
5*n² + 4and5*n² - 4for the given number n - Checking if either of these values is a perfect square
- If at least one is a perfect square, then n is a Fibonacci number
Testing with Larger Numbers
Let's test the function with some larger Fibonacci numbers ?
import math
def isPerfectSquare(x):
s = int(math.sqrt(x))
return s * s == x
def isFibonacci(n):
return isPerfectSquare(5*n*n + 4) or isPerfectSquare(5*n*n - 4)
# Test with known Fibonacci numbers
fibonacci_numbers = [13, 21, 34, 55, 89]
non_fibonacci = [14, 22, 35, 56, 90]
print("Testing known Fibonacci numbers:")
for num in fibonacci_numbers:
result = "?" if isFibonacci(num) else "?"
print(f"{num}: {result}")
print("\nTesting non-Fibonacci numbers:")
for num in non_fibonacci:
result = "?" if isFibonacci(num) else "?"
print(f"{num}: {result}")
Testing known Fibonacci numbers: 13: ? 21: ? 34: ? 55: ? 89: ? Testing non-Fibonacci numbers: 14: ? 22: ? 35: ? 56: ? 90: ?
Conclusion
This mathematical approach provides an efficient way to check if a number is Fibonacci without generating the entire sequence. The method uses the property that a number n is Fibonacci if either (5*n² + 4) or (5*n² - 4) is a perfect square.
