Math Fundamentals18 sections · 814 units
Open in Course

Pow(x, n) - Implementation

(12 lines)

function myPow(x, n):
    if n < 0:
        x = 1 / x
        n = -n
    result = 1
    while n > 0:
        if n % 2 == 1:
            result = result * x
        x = x * x
        n = n / 2
    return result