- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How to check if a given number is a Fibonacci number in Python Program ?
In this article, we will learn about the solution to the problem statement given below −
Given a number n, check whether n is a Fibonacci number or not
We all are aware that the nth Fibonacci number is the sum of the previous two Fibonacci numbers. But they also offer an interesting relation other than the recurrence relation.
A number is Fibonacci in nature if and only if (5*n2 + 4) or (5*n2 – 4) is a perfect square.
We will use this property to check whether a number is Fibonacci or not.
Now let’s see the implementation of the Python script −
import math # if x is perfect square def isPerfectSquare(x): s = int(math.sqrt(x)) return s*s == x # if n is a Fibinacci Number def isFibonacci(n): #if one of 5*n*n + 4 or 5*n*n - 4 or both is a perferct square return isPerfectSquare(5*n*n + 4) or isPerfectSquare(5*n*n - 4) for i in range(1,11): if (isFibonacci(i) == True): print (i,"is a Fibonacci Number") else: print (i,"is a not Fibonacci Number")
1 is a Fibonacci Number 2 is a Fibonacci Number 3 is a Fibonacci Number 4 is a not Fibonacci Number 5 is a Fibonacci Number 6 is a not Fibonacci Number 7 is a not Fibonacci Number 8 is a Fibonacci Number 9 is a not Fibonacci Number 10 is a not Fibonacci Number
All functions and variables are declared in the global frame as shown in the image below −
In this article, we learned the solution of identifying that the given number is Fibonacci or not.
- Related Articles
- Python Program for How to check if a given number is a Fibonacci number?
- Java Program for check if a given number is Fibonacci number?
- Program to check given number is a Fibonacci term in Python
- Python program to check if the given number is a Disarium Number
- Python program to check if a given string is number Palindrome
- Python program to check if the given number is Happy Number
- Python Program to Check if a Number is a Perfect Number
- Python Program to Check if a Number is a Strong Number
- Check if a given string is a valid number in Python
- Golang Program to Check if a Number is a Perfect Number
- Java program to print Fibonacci series of a given number.
- C program to find Fibonacci series for a given number
- Check if given number is Emirp Number or not in Python
- Python program to check if a number is Prime or not