time complexity
power function
algorithm analysis
duplicate question
computational efficiency

Time complexity of power

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Computing power values seems trivial, but algorithm choice changes complexity dramatically for large exponents. A naive loop scales linearly, while exponentiation by squaring scales logarithmically. Understanding both helps when writing custom math code, analyzing interview solutions, or optimizing systems with repeated exponent operations.

Core Sections

Naive Repeated Multiplication

The straightforward approach multiplies base by itself exponent times.

python
1def power_naive(x: int, n: int) -> int:
2    result = 1
3    for _ in range(n):
4        result *= x
5    return result
6
7print(power_naive(2, 10))

Time complexity is proportional to exponent, so this is linear time in n. Space usage is constant for iterative version.

Exponentiation by Squaring

Use identities:

  • x raised to even n equals x squared raised to n over two
  • x raised to odd n equals x times x raised to n minus one

Iterative implementation:

python
1def power_fast(x: int, n: int) -> float:
2    if n < 0:
3        return 1.0 / power_fast(x, -n)
4
5    result = 1
6    base = x
7    exp = n
8
9    while exp > 0:
10        if exp & 1:
11            result *= base
12        base *= base
13        exp >>= 1
14
15    return float(result)
16
17print(power_fast(2, 50))

Exponent is halved each loop, giving logarithmic time in exponent size.

Recursive Version and Stack Cost

Recursive fast power has same multiplication count but adds recursion stack overhead.

python
1def power_fast_rec(x: float, n: int) -> float:
2    if n == 0:
3        return 1.0
4    if n < 0:
5        return 1.0 / power_fast_rec(x, -n)
6
7    half = power_fast_rec(x, n // 2)
8    if n % 2 == 0:
9        return half * half
10    return x * half * half

Complexity is logarithmic time with logarithmic recursion depth.

Big Integer Multiplication Impact

For very large integers, multiplication itself is not constant time. True complexity depends on multiplication algorithm cost as number size grows.

So practical runtime is:

  • number of multiplications times cost per multiplication

This matters in cryptography and arbitrary-precision computations.

Modular Exponentiation

When computing powers modulo m, use modular exponentiation to keep numbers bounded.

python
1def mod_pow(x: int, n: int, m: int) -> int:
2    result = 1
3    x %= m
4
5    while n > 0:
6        if n & 1:
7            result = (result * x) % m
8        x = (x * x) % m
9        n >>= 1
10
11    return result
12
13print(mod_pow(2, 1000, 1000000007))

This still uses logarithmic exponent steps and avoids massive intermediate values.

Built-in pow and Practical Advice

In Python, pow is highly optimized and should be default for production unless custom behavior is required.

python
print(pow(2, 100))
print(pow(2, 100, 1000000007))

Knowing algorithmic complexity still matters for reasoning about system-level cost.

Benchmarking Patterns

When comparing implementations, include varying exponent scales and warmup runs.

python
1import time
2
3for e in [10_000, 100_000, 1_000_000]:
4    t0 = time.perf_counter()
5    power_fast(2, e)
6    print(e, time.perf_counter() - t0)

Focus on growth trend, not one absolute timing number.

Common Pitfalls

  • Assuming all power implementations are linear regardless of method.
  • Ignoring negative exponent handling in integer-focused implementations.
  • Comparing recursive and iterative versions without considering stack overhead.
  • Forgetting multiplication cost growth for very large integer operands.
  • Re-implementing modular power manually when optimized built-in support exists.

Summary

  • Naive power is linear time in exponent.
  • Exponentiation by squaring is logarithmic time in exponent.
  • Recursive and iterative fast methods differ mainly in stack behavior.
  • Big integer multiplication cost affects practical runtime at large scales.
  • Use built-in pow when possible and benchmark with realistic exponent ranges.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.