Skip to content

Resource · Guide or article

Fast Powering Algorithm

Shows how to compute powers by repeated halving of the exponent instead of repeated multiplication. Directly relevant to pow(x, n) interview questions and to divide and conquer basics.

Reviewed

Open the original: Fast Powering Algorithm (opens the original site in a new tab)

Add to a roadmap

Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.

Source content: English

The power of a number says how many times to use the number in a multiplication. It is written as a small number to the right and above the base number. We multiply a to itself, b times. That is, a^b = a * a * a * ... * a (b occurrences of a).

PrepKit overview

PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.

Suited to
The source does not state a level
Format
Reference

What it covers

  • Naive repeated multiplication described as O(n)
  • Even and odd exponent rules, with 2^4 and 2^5 as examples
  • Caching the half power so it is computed once, bringing the time to O(log(n))

How to use it

PrepKit study advice, not a claim made by the source.

Implement the recursive version, then an iterative one that reads the exponent's bits. Extend it to negative exponents and to a modulo variant, and test both against built-in exponentiation.

Scope and limitations

The README states it works only for two positive integers. It does not cover negative exponents, modular exponentiation or an iterative version.

Access and format

Free English README on GitHub in the MIT-licensed trekhleb/javascript-algorithms repository.

Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88

Evidence behind this overview
  • src/algorithms/math/fast-powering/README.md

Pages that include this

Get the PrepKit newsletter

Optional. You don't need it to use PrepKit.

How we handle your email