-3

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);
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Mike
  • 91
  • 2
  • 10
  • 5
    This question looks an awful lot like you are trying to do homework and don't understand the basic concepts of Big-O to answer your own questions. While StackOverflow is intended for programming questions, there is the expectation that people asking questions do some research of their own before asking questions. Please see http://stackoverflow.com/help/how-to-ask – rodamn Sep 15 '15 at 20:45
  • 1
    I'm flagging to close as Too Broad. – AStopher Sep 15 '15 at 20:56
  • I am new to this website – Mike Sep 15 '15 at 21:39

4 Answers4

4

Yes, it is even in O(n) and therefore also in O(2^n).

Waggili
  • 371
  • 1
  • 7
  • So, let say n = 3; if Big-O(n) then it runs 3 times, but if Big-O(2^n), it runs 8 times instead right? plz correct me if I am wrong. Thanks – Mike Sep 15 '15 at 20:20
  • @Mike No, Big-O notation is much more complex than that. Look it up at the Wikipedia: https://en.wikipedia.org/wiki/Big_O_notation – Waggili Sep 15 '15 at 20:23
  • @Mike: First of all, it just gives an *upper* bound, which is the reason, why the implication in my answer works. Second of all, you are allowed to apply a multiplicative constant. Thirdly, your algorithm must only respect that bound for problems bigger than a minimum size. – Waggili Sep 15 '15 at 20:25
  • Great!! Thanks a lot for your clarifications – Mike Sep 15 '15 at 20:29
2

To have a big-O which is proportional to the solution it should reduce to 1 i.e. it should make as many calls as the answer.

For there to be O(2^n) time complexity

public static int power(int base, int n) {
    return n == 0 ? 1 : multiply(base, power(base, n -1);
}

public static int multiply(int a, int b) {
    return a > 0 ? add(multiply(a - 1, b), b) : 0;
}

public static int add(int a, int b) {
    return a > b ? 1 + add(a -1, b) : b > 0 ? 1 + add(0, b -1) : 0;
}

To calculate 2^n it will reduce down to 1 + 2^n times.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 2
    :) there is an easier way though, just use return exponent(n - 1) + exponent(n - 1); instead of return 2 * exponent(n - 1); in code in question. – rsutormin Sep 16 '15 at 00:30
1

Good news: it runs in O(n).

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

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
1

This algorithm has O(n) time complexity. If you add a second parameter, you can make a method with O(log n) time complexity.

public static int power(int base, int n) {
    if (n == 0)
        return 1;
    else if (n % 2 == 0)
        return power(base * base, n/2);
    else
        return base * power(base * base, n/2);
}

In your code, n is reduced by 1 every time, so it takes n steps to terminate. With this approach, n is halved every time, so it can finish much more quickly.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116