📜  检查数字是否为完整斐波那契

📅  最后修改于: 2021-04-24 05:44:30             🧑  作者: Mango

给定数字N ,任务是检查给定数字及其所有数字是否为斐波那契。如果是这样,则给定的数字是完整的斐波那契数,否则不是。
例子:

方法:
首先检查N的所有数字是否都是斐波那契。如果是这样,则当且仅当(5 * N 2 + 4)(5 * N 2 – 4)中的一个或两个是一个完美的正方形时,才根据数字为斐波那契的原则类似地检查N是否为斐波那契。
下面的代码是上述方法的实现:

C++
// C++ program to check
// if a given number is
// a Full Fibonacci
// Number or not
 
#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
// Fibonacci Number
// and false otherwise
bool isFibonacci(int n)
{
    // N is Fibonacci if one
    // of 5*N^2 + 4 or 5*N^2 - 4
    // or both is a perferct square
    return isPerfectSquare(5 * n * n + 4)
           || isPerfectSquare(5 * n * n - 4);
}
 
// Function to check digits
bool checkDigits(int n)
{
    // Check if all digits
    // are fibonacci or not
    while (n) {
 
        // Extract digit
        int dig = n % 10;
 
        // Check if the current
        // digit is not fibonacci
        if (dig == 4 && dig == 6
            && dig == 7 && dig == 9)
            return false;
 
        n /= 10;
    }
 
    return true;
}
 
// Function to check and
// return if N is a Full
// Fibonacci number or not
int isFullfibonacci(int n)
{
    return (checkDigits(n)
            && isFibonacci(n));
}
 
// Driver Code
int main()
{
    int n = 13;
    if (isFullfibonacci(n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java program to check if a given
// number is a full fibonacci
// number or not
import java.util.*;
 
class GFG {
 
// A utility function 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 and false otherwise
static boolean isFibonacci(int n)
{
     
    // N is fibonacci if one of
    //  5 * N ^ 2 + 4 or 5 * N ^ 2 - 4
    // or both is a perferct square
    return isPerfectSquare(5 * n * n + 4) ||
           isPerfectSquare(5 * n * n - 4);
}
 
// Function to check digits
static boolean checkDigits(int n)
{
     
    // Check if all digits
    // are fibonacci or not
    while (n != 0)
    {
 
        // Extract digit
        int dig = n % 10;
 
        // Check if the current
        // digit is not fibonacci
        if (dig == 4 && dig == 6 &&
            dig == 7 && dig == 9)
            return false;
 
        n /= 10;
    }
    return true;
}
 
// Function to check and return if N
// is a full fibonacci number or not
static boolean isFullfibonacci(int n)
{
    return (checkDigits(n) &&
            isFibonacci(n));
}
 
 
// Driver code
public static void main(String[] args)
{
    int n = 13;
     
    if (isFullfibonacci(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to check
# if a given number is
# a Full Fibonacci
# Number or not
from math import *
 
# A utility function that
# returns true if x is
# perfect square
def isPerfectSquare(x):
     
    s = sqrt(x)
    return (s * s == x)
 
# Returns true if N is a
# Fibonacci Number
# and false otherwise
def isFibonacci(n):
     
    # N is Fibonacci if one
    # of 5 * N ^ 2 + 4 or 5 * N ^ 2 - 4
    # or both is a perferct square
    return (isPerfectSquare(5 * n * n + 4) or
            isPerfectSquare(5 * n * n - 4))
 
# Function to check digits
def checkDigits(n):
     
    # Check if all digits
    # are fibonacci or not
    while (n):
         
        # Extract digit
        dig = n % 10
 
        # Check if the current
        # digit is not fibonacci
        if (dig == 4 and dig == 6 and
            dig == 7 and dig == 9):
            return False
 
        n /= 10
    return True
 
# Function to check and
# return if N is a Full
# Fibonacci number or not
def isFullfibonacci(n):
     
    return (checkDigits(n) and isFibonacci(n))
 
# Driver Code
if __name__ == '__main__':
     
    n = 13
     
    if (isFullfibonacci(n)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by Samarth


C#
// C# program to check if a given
// number is a full fibonacci
// number or not
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 and false otherwise
static bool isFibonacci(int n)
{
     
    // N is fibonacci if one of
    // 5 * N ^ 2 + 4 or 5 * N ^ 2 - 4
    // or both is a perferct square
    return isPerfectSquare(5 * n * n + 4) ||
           isPerfectSquare(5 * n * n - 4);
}
 
// Function to check digits
static bool checkDigits(int n)
{
     
    // Check if all digits
    // are fibonacci or not
    while (n != 0)
    {
 
        // Extract digit
        int dig = n % 10;
 
        // Check if the current
        // digit is not fibonacci
        if (dig == 4 && dig == 6 &&
            dig == 7 && dig == 9)
            return false;
 
        n /= 10;
    }
    return true;
}
 
// Function to check and return if N
// is a full fibonacci number or not
static bool isFullfibonacci(int n)
{
    return (checkDigits(n) &&
            isFibonacci(n));
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 13;
     
    if (isFullfibonacci(n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by SoumikMondal


输出:
Yes

时间复杂度: O(1)

辅助空间: O(1)