Skip to main content
Commonmark migration
Source Link

#Java 8, 94 bytes

Java 8, 94 bytes

x->{int i=0;for(;c(i++)<=x;);return c(i-2)==x;}int c(int n){return n<1?0:n<2?1:c(n-1)+c(n-2);} 

Explanation:

Try it here. (NOTE: It's a bit slow for very large test-cases.)

x->{ // Method (1) with integer parameter and boolean return-type int i=0; // Index for(;c(i++)<=x;); // Loop as long as the Fibonacci number is smaller or equal to the input return c(i-2)==x; // And then return if the input equals the previous Fibonacci number } // End of method (1) // Method to get `n`th Fibonacci number int c(int n){ // Method (2) with integer parameter and integer return-type return n<1? // If `n`==0: 0 // Return 0 :n<2? // Else if `n`==1 1 // Return 1 : // Else: c(n-1)+c(n-2); // Return recursive calls with `n-1` and `n-2` } // End of method (2) 

#Java 8, 94 bytes

x->{int i=0;for(;c(i++)<=x;);return c(i-2)==x;}int c(int n){return n<1?0:n<2?1:c(n-1)+c(n-2);} 

Explanation:

Try it here. (NOTE: It's a bit slow for very large test-cases.)

x->{ // Method (1) with integer parameter and boolean return-type int i=0; // Index for(;c(i++)<=x;); // Loop as long as the Fibonacci number is smaller or equal to the input return c(i-2)==x; // And then return if the input equals the previous Fibonacci number } // End of method (1) // Method to get `n`th Fibonacci number int c(int n){ // Method (2) with integer parameter and integer return-type return n<1? // If `n`==0: 0 // Return 0 :n<2? // Else if `n`==1 1 // Return 1 : // Else: c(n-1)+c(n-2); // Return recursive calls with `n-1` and `n-2` } // End of method (2) 

Java 8, 94 bytes

x->{int i=0;for(;c(i++)<=x;);return c(i-2)==x;}int c(int n){return n<1?0:n<2?1:c(n-1)+c(n-2);} 

Explanation:

Try it here. (NOTE: It's a bit slow for very large test-cases.)

x->{ // Method (1) with integer parameter and boolean return-type int i=0; // Index for(;c(i++)<=x;); // Loop as long as the Fibonacci number is smaller or equal to the input return c(i-2)==x; // And then return if the input equals the previous Fibonacci number } // End of method (1) // Method to get `n`th Fibonacci number int c(int n){ // Method (2) with integer parameter and integer return-type return n<1? // If `n`==0: 0 // Return 0 :n<2? // Else if `n`==1 1 // Return 1 : // Else: c(n-1)+c(n-2); // Return recursive calls with `n-1` and `n-2` } // End of method (2) 
Source Link
Kevin Cruijssen
  • 136.3k
  • 14
  • 155
  • 394

#Java 8, 94 bytes

x->{int i=0;for(;c(i++)<=x;);return c(i-2)==x;}int c(int n){return n<1?0:n<2?1:c(n-1)+c(n-2);} 

Explanation:

Try it here. (NOTE: It's a bit slow for very large test-cases.)

x->{ // Method (1) with integer parameter and boolean return-type int i=0; // Index for(;c(i++)<=x;); // Loop as long as the Fibonacci number is smaller or equal to the input return c(i-2)==x; // And then return if the input equals the previous Fibonacci number } // End of method (1) // Method to get `n`th Fibonacci number int c(int n){ // Method (2) with integer parameter and integer return-type return n<1? // If `n`==0: 0 // Return 0 :n<2? // Else if `n`==1 1 // Return 1 : // Else: c(n-1)+c(n-2); // Return recursive calls with `n-1` and `n-2` } // End of method (2)