• + 1 comment

    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);
    }