Search code examples
javaalgorithmdivide-and-conquer

Java program to compute the value of (2^n); the program should run with Big-O(2^n)


Is this java program runs with Big-O(2^n)? if not, any suggestions on how to modify it?

This program calculates the value of 2^n:

public static int exponent(int n) {
    if (n == 0)
        return 1;   
    else
        return 2 * exponent(n - 1);
}

Solution

  • Good news: it runs in O(n).

    Each iteration, n is decremented by 1, so it loops exactly n times