Power algorithm, Fast Power Algorithm

Source: Internet
Author: User

Power algorithm, Fast Power Algorithm

1. simple recursion

The simplest power algorithm is based on xn = x * xn-1, using recursion:

def foo(x,n):    if n==0:        return 1    else:        return x*foo(x,n-1)

In this way, the nth power of x is obtained, n-1-th Multiplication operation is performed. The efficiency is very low when n is large.

2. Efficient Recursion

A more efficient algorithm that can reduce the number of computations to the LogN level, because:

Xn = xn/2 * xn/2, where n is an even number

Xn = x (n-1)/2 * x (n-1)/2 * x, when n is an odd number

def foo(x,n):    if n==0:        return 1    else:        if n%2==0:            return foo(x,n/2)*foo(x,n/2)        else:            return foo(x,n/2)*foo(x,n/2)*x

The operation count can be reduced to the LogN level.

3. Quick Power Determination

There is also a fast power algorithm called the binary method:

For example, to obtain the power of x 21, the binary value of 21 is 10101. Because x21 = x16 * x4 * x1, we can see that it is expressed in binary format (10101). When 1 is encountered, x is multiplied by x, forward from right to left, x is automatically multiplied.

def foo(x,n):    result=1    while n:        if (n&1):            result*=x        x*=x        n>>=1    return result

This algorithm is very efficient when used to calculate a very large number. For example, many prime number testing algorithms depend on different variants of this algorithm.

4. Abstraction

Then I saw a very interesting idea somewhere, that is, to convert the self-multiplication function in the above algorithm:

def foo(x,n,i,func):    result=i    while n:        if (n&1):            result=func(result,x)        x=func(x,x)        n>>=1    return resultif __name__=='__main__':    print foo(2,16,1,lambda x,y:x*y)

In this way, the Npower of x can be computed using foo (x, n, 1, lambda x, y: x * y.

If x * n is obtained, it is equivalent to adding x to n times. You can use foo (x, n, 0, lambda x, y: x + y) to calculate:

def foo(x,n,i,func):    result=i    while n:        if (n&1):            result=func(result,x)        x=func(x,x)        n>>=1    return resultif __name__=='__main__':    print foo(2,16,0,lambda x,y:x+y)

If you want to repeat a character x n times, you can use:

def repeat(x,n,i,func):    result=i    while n:        if (n&1):            result=func(result,x)        x=func(x,x)        n>>=1    return resultif __name__=='__main__':    print repeat('a',16,'',lambda x,y:x+y)

Copy a list n times:

def repeat(x,n,i,func):    result=i    while n:        if (n&1):            result=func(result,x)        x=func(x,x)        n>>=1    return resultif __name__=='__main__':    print repeat([1,2],16,[],lambda x,y:x+y)

Reference: http://videlalvaro.github.io/2014/03/the-power-algorithm.html


Power Operation in c #

Math. Pow (2, 2)

Know the base number and exponent, power. What is this operation called?

This operation is called multiplication. In a ^ n, the same multiplier a is called the base number, the number n of a is called the index, and the result of the multiplication is called the power. A ^ n reads the n power of.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.