Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to Check if a Given Number is Fibonacci number - Python
Next article icon

How to Check if a Given Number is Fibonacci number - Python

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Fibonacci numbers are part of a famous sequence where each number is the sum of the two preceding ones, i.e. F(n) = F(n-1) + F(n-2). The sequence starts as:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

Notice that every number is equal to the sum of its previous 2 numbers.

In this article, we will learn how to identify if a given number belongs to the Fibonacci series or not.

Examples : 

Input: 8
Output: Yes

Input: 31
Output: No

Fibonacci Number Check Using a Mathematical Property

A number n is a Fibonacci number if and only if one or both of (5*n² + 4) or (5*n² – 4) is a perfect square.

The above mathematical expression is derived from the closed-form expression of Fibonacci numbers (Binet’s Formula) and some number theory. It’s fast and doesn’t require generating the Fibonacci sequence. Let's look at the code implementation in Python:

Python
import math  def is_perfect_sq(x):     s = int(math.sqrt(x))     return s * s == x  def is_fibonacci(n):     return is_perfect_sq(5 * n * n + 4) or is_perfect_sq(5 * n * n - 4)  for i in range(1, 7):     if is_fibonacci(i):         print(f"{i} is a Fibonacci Number")     else:         print(f"{i} is not a Fibonacci Number") 

Output
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 

Explanation:

1. is_perfect_sq(x):

  • Calculates the integer square root of x.
  • Returns True if x is a perfect square, else False.

2. is_fibonacci(n):

  • Applies the mathematical identity:
  • A number n is Fibonacci if 5*n² + 4 or 5*n² – 4 is a perfect square.
  • Calls is_perfect_sq() on both expressions to check this.

3. Loop: Iterates through numbers 1 to 6 and prints whether each number is a Fibonacci number based on the result from is_fibonacci().

Please refer this complete article on How to check if a given number is Fibonacci number? for more details! 


Next Article
How to Check if a Given Number is Fibonacci number - Python

K

kartik
Improve
Article Tags :
  • Python
Practice Tags :
  • python

Similar Reads

    Nth multiple of a number in Fibonacci Series in Python
    Our task is to find the nth multiple of a number in the Fibonacci series which involves identifying Fibonacci numbers that are exactly divisible by a given number m. This means we will check each Fibonacci number one by one and select only those that are multiples of m. Once we find the nth such num
    4 min read
    Check If Value Is Int or Float in Python
    In Python, you might want to see if a number is a whole number (integer) or a decimal (float). Python has built-in functions to make this easy. There are simple ones like type() and more advanced ones like isinstance(). In this article, we'll explore different ways to check if a value is an integer
    4 min read
    Check if a given string is binary string or not - Python
    The task of checking whether a given string is a binary string in Python involves verifying that the string contains only the characters '0' and '1'. A binary string is one that is composed solely of these two digits and no other characters are allowed. For example, the string "101010" is a valid bi
    3 min read
    Implement IsNumber() function in Python
    In this article we will see how to implement isNumber() method using Python. This method takes in a string as input and returns True or False according to whether the string is a number or not. Examples: Input : "12345" Output : True Input : "-12345" Output : True Input : "abc" Output : False Approa
    2 min read
    Check If String is Integer in Python
    In this article, we will explore different possible ways through which we can check if a string is an integer or not. We will explore different methods and see how each method works with a clear understanding.Example:Input2 : "geeksforgeeks"Output2 : geeksforgeeks is not an IntigerExplanation : "gee
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences