Analyze different powers

Report a typo

Raising a number to some power is a very frequent task in mathematics. Now, we want to implement this power operation using programming. Let's have a look at the solution below:

function pow(x, n):
    powers = array of size (n+1)
    for (i = 0; i <= n; i = i + 1):
        powers[i] = -1
    return power_helper(x, n, powers)


function power_helper(x, n, powers):
    if powers[n] != -1:
        return powers[n]

    if n == 0 then
        powers[n] = 1


    if n is odd then
        powers[n] = x * power_helper(x, (n-1) / 2, powers) * power_helper(x, (n-1) / 2, powers)
    else:
        powers[n] = power_helper(x, n / 2, powers) * power_helper(x, n / 2, powers)
    
    return powers[n]

Please select the options appropriate to the program.

Select one or more options from the list
___

Create a free account to access the full topic