Math Fundamentals18 sections · 814 units
Open in Course

Pow(x, n) - Algorithm

(Fast exponentiation with negatives)

1.1. If n < 0, set x = 1 / x and n = -n.

2.2. Initialize result = 1.

3.3. While n > 0: if n is odd, multiply result by x. Square x. Divide n by 2.

4.4. Return result.