Algorithm dossier
- Category: Math
- Worst-case complexity: O(log n)
- Approach: Iterative
- Data structure: No DS
- First formalised: 1960s
Why this snippet is Fast (Binary) Exponentiation
Why binary exponentiation. Walk the bits of the exponent n low-to-high. Whenever the current bit is 1, fold the current a into the result. After each bit, square a (so it represents the next power-of-two base). The n >>= 1 consumes bits one at a time. Why it's log. Number of iterations = bits in n = O(log n). Modular form (with % m) is the workhorse inside RSA and Diffie-Hellman.
How to read a redacted algorithm
Algodle strips identifier names so the snippet has to be read for its shape: the control flow, the data structures it manipulates, the order in which it visits its input. Loops with two pointers crawling toward each other are usually search or partition. A recursion that splits its input in half and recurses on both halves is divide-and-conquer. A priority queue plus graph traversal is almost certainly Dijkstra, Prim, or A*. Six hint columns — category, complexity, approach, data structure, era — let you triangulate even when the snippet itself is opaque.