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);
}
Good news: it runs in O(n)
.
Each iteration, n
is decremented by 1
, so it loops exactly n
times