We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Mathematics
- Fundamentals
- Is Fibo
- Discussions
Is Fibo
Is Fibo
+ 0 comments C# Solution
public static string isFibo(long n) { long first = 0; long second = 1; for(long i = 0; i < n; i++) { long temp = second; second += first; first = temp; if(second == n) return "IsFibo"; if(second > n) return "IsNotFibo"; } return "IsNotFibo"; }
+ 0 comments This is my Java 8 solution, feel free to ask me any questions.
Solution 1:public static String isFibo(long n) { long first = 0; long second = 1; long temporary = 0; while(temporary < n) { temporary = first + second; first = second; second = temporary; } if(n == temporary) return "IsFibo"; return "IsNotFibo"; }
Solution 2:
public static String isFibo(long n) { if(isFibo(n, 0, 1)) return "IsFibo"; return "IsNotFibo"; } private static boolean isFibo(long n, long first, long second) { // Basecase: checking if the sum of f(n-1) + f(n-2) // Return false if Sum is larger than N // Return true if Sum is N long sum = first + second; if(sum == n) return true; if(sum > n) return false; // Recursive call return isFibo(n, second, sum); }
+ 0 comments If you're aware that the Fibonacci series grows even faster than exponentially, it's pretty simple to brute-force this one.
+ 0 comments JavaScript:
function isFibo(n) { // Write your code here let fibonacciSequence = [0,1]; let lastFibonacci = 1; while(lastFibonacci <= n){ if(lastFibonacci === n) return "IsFibo"; fibonacciSequence.push(lastFibonacci); lastFibonacci = fibonacciSequence.at(-2) + fibonacciSequence.at(-1); } return "IsNotFibo"; }
+ 0 comments def isFibo(n): # Write your code here start = 0 last = 1 while last <= n: tmp = last + start start = last last = tmp if last== n: return "IsFibo" return "IsNotFibo"
Load more conversations
Sort 382 Discussions, By:
Please Login in order to post a comment