📌  相关文章
📜  如何检查给定数字是否为斐波那契数?

📅  最后修改于: 2021-05-06 21:07:13             🧑  作者: Mango

给定数字“ n”,如何检查n是否为斐波那契数。前几个斐波那契数是0、1、1、2、3、5、8、13、21、34、55、89、144 、.
例子 :

Input : 8
Output : Yes

Input : 34
Output : Yes

Input : 41
Output : No

一种简单的方法是生成斐波那契数,直到生成的数大于或等于“ n”。以下是有关斐波那契数的一个有趣属性,也可以用于检查给定数字是否为斐波那契。
当且仅当(5 * n 2 + 4)或(5 * n 2 – 4)中的一个或两个是一个完美的正方形时,这个数字才是斐波那契数(来源:Wiki)。以下是基于此概念的简单程序。

C++
// C++ program to check if x is a perfect square
#include 
#include 
using namespace std;
 
// A utility function that returns true if x is perfect square
bool isPerfectSquare(int x)
{
    int s = sqrt(x);
    return (s*s == x);
}
 
// Returns true if n is a Fibinacci Number, else false
bool isFibonacci(int n)
{
    // n is Fibinacci if one of 5*n*n + 4 or 5*n*n - 4 or both
    // is a perferct square
    return isPerfectSquare(5*n*n + 4) ||
           isPerfectSquare(5*n*n - 4);
}
 
// A utility function to test above functions
int main()
{
  for (int i = 1; i <= 10; i++)
     isFibonacci(i)? cout << i << " is a Fibonacci Number \n":
                     cout << i << " is a not Fibonacci Number \n" ;
  return 0;
}


Java
// Java program to check if x is a perfect square
 
class GFG
{
    // A utility method that returns true if x is perfect square
    static  boolean isPerfectSquare(int x)
    {
        int s = (int) Math.sqrt(x);
        return (s*s == x);
    }
      
    // Returns true if n is a Fibonacci Number, else false
    static boolean isFibonacci(int n)
    {
        // n is Fibonacci if one of 5*n*n + 4 or 5*n*n - 4 or both
        // is a perfect square
        return isPerfectSquare(5*n*n + 4) ||
               isPerfectSquare(5*n*n - 4);
    }
 
    // Driver method
    public static void main(String[] args)
    {
        for (int i = 1; i <= 10; i++)
             System.out.println(isFibonacci(i) ?  i +  " is a Fibonacci Number" :
                                                  i + " is a not Fibonacci Number");
    }
}
//This code is contributed by Nikita Tiwari


Python
# python program to check if x is a perfect square
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 Fibinacci Number, else false
def isFibonacci(n):
 
    # n is Fibinacci 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)
    
# A utility function to test above functions
for i in range(1,11):
     if (isFibonacci(i) == True):
         print i,"is a Fibonacci Number"
     else:
         print i,"is a not Fibonacci Number "


C#
// C# program to check if
// x is a perfect square
using System;
 
class GFG {
 
    // A utility function that returns
    // true if x is perfect square
    static bool isPerfectSquare(int x)
    {
        int s = (int)Math.Sqrt(x);
        return (s * s == x);
    }
 
    // Returns true if n is a
    // Fibonacci Number, else false
    static bool isFibonacci(int n)
    {
        // n is Fibonacci if one of
        // 5*n*n + 4 or 5*n*n - 4 or
        // both are a perfect square
        return isPerfectSquare(5 * n * n + 4) ||
               isPerfectSquare(5 * n * n - 4);
    }
 
    // Driver method
    public static void Main()
    {
        for (int i = 1; i <= 10; i++)
            Console.WriteLine(isFibonacci(i) ? i +
                              " is a Fibonacci Number" : i +
                              " is a not Fibonacci Number");
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


输出:

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