Math Fundamentals18 sections · 814 units
Open in Course

Power - Complexity Analysis (Why it's logarithmic)

Time and space breakdown

Each recursive call reduces nn by half (when even) or by 11 (when odd). In the worst case, you halve nn about log2(n)\log_2(n) times before reaching 00.

Time complexity: O(logn)O(\log n). You make at most log2(n)\log_2(n) recursive calls, and each call does O(1)O(1) work (one multiplication).

Space complexity: O(logn)O(\log n) for the recursion stack. You can convert this to an iterative solution to reduce space to O(1)O(1), but the time stays O(logn)O(\log n).