Binary exponentiation

Build a power from factors. For 3¹³ you need just three: 3⁸ × 3⁴ × 3.

Square the factor: 3 becomes 9, then 81. Its exponent doubles each time.

Start at 1. A zero exponent needs no factors, so the result is 1, including for 0⁰.

One pass per binary digit takes O(log n) time for n > 1 and O(1) extra space.