Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 08 · DSA · Worked problems, in English · about 20 min to read

Play it as a journeyTutor comes with a plan

Clinic #31: Pow(x, n)

Pattern: Fast exponentiation · LC 50

Thinking in Bits
Thinking in Bits · Open board

Shows a number as a row of switches, and the moves a computer makes on that row in one step. This clinic uses two of them, “is the lowest switch on?” and “slide the row one place right”, to raise a number to a two-billionth power in 31 steps.

Before you start. Bit Manipulation (thinking in bits) showed that every whole number is stored as a row of 1s and 0s, and that sliding the row one place to the right halves the number, dropping any remainder. Complexity & Big-O gave the other idea you need: an answer whose cost goes up by one step each time the input doubles is called O(log n).

Lesson 70 of 96 in DSA

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • A calculator that charges per press
  • The wrong first idea: split it in half, then do both halves
  • The click: squaring, multiplying the display by itself
  • Hitting 13 exactly: the exponent’s binary digits
  • A negative exponent: the reciprocal key
  • A calculator with a three-digit display: the modulus
  • The tell: a big exponent, or “modulo” next to a power
  • Solution ladder
  • The loop walked, printed
  • Timings (measured, 2026-09-27)
  • The −2³¹ trap, in a 32-bit box
  • Modular exponentiation: the same loop, with a modulus on every line
  • First time in an interview: the path
  • The fast exponentiation ladder: easy, medium, hard

Terms it explains: LC 50, O(log n), base, exponent, squaring, doubles, exponentiation by squaring, binary, bit, reciprocal, modulus, modular exponentiation, read the bits, iterative, O(1), BEST, Code the loop, Count Good Numbers, 6, 10, 18, 10.

Review DSA