0

I was wondering what the computational complexity of this function would be?

2^(log(n)-1)

the log is base 2.

dgamma3
  • 2,333
  • 4
  • 26
  • 47
  • you already have the answer. O(2^logn). which is exponential. – DarthVader Sep 04 '12 at 05:21
  • @DarthVader nope. OP asks for the CC of this function itself. –  Sep 04 '12 at 05:22
  • @DarthVader oh, and if you raise a number to a logiarithm whose base is the number itself, it's by no means exponential, it's linear. –  Sep 04 '12 at 05:23

1 Answers1

2

It depends on using what algorithm you calculate all the logarithms and powers. If you're smart enough to notice that this function is essentially a division by 2, then you can implement this in constant time (i. e. O(1)) for integers by doing a right shift.

  • thanks for that. so because the number would be divisible by 2, how does that make it O(1) – dgamma3 Sep 04 '12 at 05:25
  • Yeah, this is kind of a weird question. It's sort of like asking: what the complexity of this function: Sort(). :) – aquinas Sep 04 '12 at 05:25
  • @dgamma3 read the answer carefully. (and it doesn't need to be divisible by 2). –  Sep 04 '12 at 05:26
  • @H2CO3 sorry, could you explain. – dgamma3 Sep 04 '12 at 05:27
  • @dgamma3 what this function essentially does is a division by two (remember highschool math? Great.) To divide an integer by two, you can perform a right shift on it (by 1 bit) which is a constant-time operation. (Hint: you should probably not be asking anout CC yet if you don't know what a right shift is, or even what this function does.) –  Sep 04 '12 at 05:30