Check if the number is Fibonacci | Practice | GeeksforGeeks

Check if a given number N is the Fibonacci number. A Fibonacci number is a number that occurs in the Fibonacci series.
 
Example 1:
Input:
N = 34
Output:
Yes
Explanation:
34 is one of the numbers 
of the Fibonacci series.
 
Example 2:
Input:
N = 41
Output:
No
Explanation:
41 is one of the numbers 
of the Fibonacci series.
 
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkFibonacci() which takes an integer N and returns a string "Yes" if it is a Fibonacci number else "No".
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 100
 
0
mohdraza2 months ago
0.1simport java.io.*;import java.util.*;
class GFG{   public static void main(String args[])throws IOException   {       BufferedReader read = new BufferedReader(new InputStreamReader(System.in));       int t = Integer.parseInt(read.readLine());       while(t-- > 0)       {           int N = Integer.parseInt(read.readLine());           Solution ob = new Solution();           System.out.println(ob.checkFibonacci(N));       }   }}// } Driver Code Ends
//User function Template for Javaclass Solution{   static String checkFibonacci(int N){     int x=0,y=1;     int z=0;     String ans="";     if(N==2||N==3||N==1||N==0||N==5){         return "Yes";     }     for(int i=0;i<=N/2;i++){         z=x+y;         x=y;         y=z;         if(z!=N)         {             ans="No";             continue;         }
+1
madhucoder2 months ago
Java Code:
static String checkFibonacci(int N){
       // code here
       int x = 5*N*N+4;
       int y = 5*N*N-4;
       if((Math.sqrt(x)==Math.floor(Math.sqrt(x))) || (Math.sqrt(y)==Math.floor(Math.sqrt(y))))
       {
           return "Yes";
       }
       else
       {
           return "No";
       }
   }
0
madhucoder
This comment was deleted.
0
chechipresh2 months ago

class Solution{   
public:
    string checkFibonacci(int N){
        // code here 
        if (N==0 || N==1)
            return "Yes";
        int a=0,b=1;
        int s=a+b;
        while (s<N){
            a=b;
            b=s;
            s=a+b;
            if (s==N)
                return "Yes";
        }
        return "No";
    }
};

0
ramdurgasais2 months ago
 IN python
def checkFibonacci (ob,N):
        # code here 
        a, b = 0, 1 
        if N in (a,b) : return "Yes"
        while N > b:
            a, b = b, a+b
            if b == N : return "Yes"
        return "No"
0
nikhillko023 months ago
def checkFibonacci(ob,N):       # code here       l=[0 for i in range(N+3)]       if N==0 or N==1 or N==2:           return "Yes"       l[1]=1       for i in range(2,N+3):           l[i]=l[i-1]+l[i-2]       if N in l:           return "Yes"       else:           return "No"
0
pranavtripathi20013 months ago
string checkFibonacci(int N){       int x=0,y=1;       int z=0;       string ans="";       for(int i=0;i<=N;i++)       {           z=x+y;           x=y;           y=z;                                    if(z==N)       {           ans= "Yes";           break;       }       else{           ans= "No";       }              }   return ans;       }
+1
mdmozammilraza064 months ago
class Solution{   static String checkFibonacci(int N){       // code here       int a = 0;       int b = 1;       int c = 0;       String ans = "";       for (int i=0; i<=N; i++)       {           c = a+b;           a = b;           b = c;           if (c == N)           {               ans = "Yes";                 break;           }           else           {               ans = "No";           }       }       return ans;   }} 
0
mohammadadnaan20034 months ago
 int ans = (N * (N+1)) / 2;  for(int i=1;i<=ans;i++)  {      for(int j=1;j<=N;j++)      {          cout<<i<<" ";      }      cout<<endl;  }
+1
mayank20214 months ago
#python3    def checkFibonacci (ob,N):       # code here        i=1       j=1       while N > j :           temp=i           i=j           j=temp+j       if N <j:           return "No"       elif N==j:           return "Yes"
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.