Divide the power

Report a typo

We are all familiar with the power operation in mathematics, for example, 26=642^6 = 64. Now, we want to implement this power operation using programming. Here are some methods to implement this:

1.

function power(x, n):
    pow = 1
    for i from 1 to n:
        pow = pow * x

    return pow

2.

function power(x, n):
    if n == 0 then:
        return 1
    if n is odd then:
        return x * power(x, (n-1) / 2) * power(x, (n-1) / 2)
    else:
        return power(x, n / 2) * power(x, n / 2)

3.

function power(x, n):
    if n = 0 then:
        return 1
    return x * power(x, n-1)

Which of the preceding methods has been implemented using the divide and conquer paradigm?

Select one option from the list
___

Create a free account to access the full topic