How to Check if a Given Number is Fibonacci number - Python Last Updated : 23 Jul, 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: 8Output: YesInput: 31Output: NoFibonacci Number Check Using a Mathematical PropertyA 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") Output1 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! Comment More infoAdvertise with us Next Article Must Do Coding Questions - Topic-wise K kartik Follow Improve Article Tags : Python Practice Tags : python Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like