VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 08: Recursion and backtracking

Lesson 8.2 of 10 in this module Unrolled Pro

Pow(x, n)

Compute x to the power n for any integer n, including negative and very large n. From multiplying n times (O(n)), to recursive halving where x^n = (x^(n/2))², with the trap of computing the half twice, to iterative binary exponentiation that reads the bits of n (O(log n) time, O(1) space). Then modular exponentiation, matrix powers that give the n-th Fibonacci number in O(log n), n-step probabilities of a Markov chain, and log space when a product of probabilities underflows to zero.

about 10 min 7 Python examples 2 quick checks

Problem. Implement my_pow(x, n) = xⁿ for a float x and an integer n, which can be negative and as large as 2³¹ − 1 in absolute value.

text
my_pow(2.0, 10)    →  1024.0
my_pow(2.1, 3)     →  9.261
my_pow(2.0, -2)    →  0.25

The recursion tree here isn't a wide search tree but a single chain that halves the problem at each level: the cheapest kind of recursion, and a pattern that works for anything you can multiply associatively.

ML context. Repeated multiplication by the same thing is everywhere: the n-step transition probabilities of a Markov chain are the n-th power of its matrix, a linear recurrence is a matrix power, decayed learning rates and exponential moving averages use γⁿ, and modular exponentiation underlies hashing and cryptography. And multiplying many small probabilities is where floats break, which is why models work in log space (Change D).


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Pow(x, n)"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee