Check if a number is Full Fibonacci or not
Last Updated :
25 Sep, 2022
Given a number N, the task is to check if the given number and all its digits are Fibonacci. If so, then the given number is a Full Fibonacci Number, else not.
Examples:
Input: 13
Output: Yes
Explanation: 13 and its digits 1 and 3 are all Fibonacci numbers
Input: 34
Output: No
Explanation: 4 is not a Fibonacci number.
Approach:
First check if all digits of N are Fibonacci or not. If so, similarly check if N is Fibonacci or not by the principle that a number is Fibonacci if and only if one or both of (5*N2 + 4) or (5*N2 – 4) is a perfect square.
Below code is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPerfectSquare( int x)
{
int s = sqrt (x);
return (s * s == x);
}
bool isFibonacci( int n)
{
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}
bool checkDigits( int n)
{
while (n) {
int dig = n % 10;
if (dig == 4 && dig == 6
&& dig == 7 && dig == 9)
return false ;
n /= 10;
}
return true ;
}
int isFullfibonacci( int n)
{
return (checkDigits(n)
&& isFibonacci(n));
}
int main()
{
int n = 13;
if (isFullfibonacci(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
static boolean isPerfectSquare( int x)
{
int s = ( int ) Math.sqrt(x);
return (s * s == x);
}
static boolean isFibonacci( int n)
{
return isPerfectSquare( 5 * n * n + 4 ) ||
isPerfectSquare( 5 * n * n - 4 );
}
static boolean checkDigits( int n)
{
while (n != 0 )
{
int dig = n % 10 ;
if (dig == 4 && dig == 6 &&
dig == 7 && dig == 9 )
return false ;
n /= 10 ;
}
return true ;
}
static boolean isFullfibonacci( int n)
{
return (checkDigits(n) &&
isFibonacci(n));
}
public static void main(String[] args)
{
int n = 13 ;
if (isFullfibonacci(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
from math import *
def isPerfectSquare(x):
s = sqrt(x)
return (s * s = = x)
def isFibonacci(n):
return (isPerfectSquare( 5 * n * n + 4 ) or
isPerfectSquare( 5 * n * n - 4 ))
def checkDigits(n):
while (n):
dig = n % 10
if (dig = = 4 and dig = = 6 and
dig = = 7 and dig = = 9 ):
return False
n / = 10
return True
def isFullfibonacci(n):
return (checkDigits(n) and isFibonacci(n))
if __name__ = = '__main__' :
n = 13
if (isFullfibonacci(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isPerfectSquare( int x)
{
int s = ( int )Math.Sqrt(x);
return (s * s == x);
}
static bool isFibonacci( int n)
{
return isPerfectSquare(5 * n * n + 4) ||
isPerfectSquare(5 * n * n - 4);
}
static bool checkDigits( int n)
{
while (n != 0)
{
int dig = n % 10;
if (dig == 4 && dig == 6 &&
dig == 7 && dig == 9)
return false ;
n /= 10;
}
return true ;
}
static bool isFullfibonacci( int n)
{
return (checkDigits(n) &&
isFibonacci(n));
}
public static void Main(String[] args)
{
int n = 13;
if (isFullfibonacci(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isPerfectSquare(x) {
var s = parseInt( Math.sqrt(x));
return (s * s == x);
}
function isFibonacci(n) {
return isPerfectSquare(5 * n * n + 4) ||
isPerfectSquare(5 * n * n - 4);
}
function checkDigits(n) {
while (n != 0) {
var dig = n % 10;
if (dig == 4 && dig == 6 && dig == 7
&& dig == 9)
return false ;
n /= 10;
}
return true ;
}
function isFullfibonacci(n) {
return (checkDigits(n) && isFibonacci(n));
}
var n = 13;
if (isFullfibonacci(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(logn)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...