"Daily algorithm" high-speed power

Source: Internet
Author: User

Integer number of values

Implementation functions

Do not use the library function for the n-th party of the base. There is no need to consider the problem of large numbers at the same time.

Tips

The problem itself is very intuitive, but the more simple the question the more need to think carefully. Includes boundary issues and efficiency issues. It is not possible to give a pleasant answer if the following 3 points are not considered:

    1. Consider the case where n is negative;
    2. Consider the case of base 0.
    3. How to ensure efficiency when n is large?
Analysis

For the above 3 questions, we answer each:

1. At the time of calculation. We uniformly calculate the ABS (n) of the base, and the final assumption is negative. The answer should be the reciprocal;

2. Assume base is 0. Then it cannot do the denominator. At this time if n<0. Then we should return the error message.

For the return error message, there are generally the following methods:

    • by the return value;
    • Through global variables.
    • Throws an exception.

Here, we notice that the return value itself can take a random value, so it cannot be simply by the return value; Assuming that only global variables are set, then there are checks after each calculation, which is more troublesome; we can choose the return value + global variable to return the error:

Assume an error, return 0, and set the global variable.

3. When n is large. High-speed power can be used:

If n is even, base^n = base^ (N/2) * base^ (N/2);
If n is an odd number. Base^n = base * base^ ((n-1)/2) * base^ ((n-1)/2);

Answer

The following are the power functions:

BOOLError =false;DoublePower (DoubleBaseintN) {error =false;if(Equal (base,0.0) && N <0) {error =true;return 0.0; }unsigned intABSN = (unsigned int) n;if(N <0) ABSN = (unsigned int) (-N);Doubleresult = Powerwithunsignedn (base, ABSN);if(N <0) result =1.0/result;returnResult;}

Notice: For decimals, infer whether equality cannot be directly used = =, but the difference between the two should be calculated within a range of accuracy:

boolequal(intint num2){    if ((num1-num2) > -0.00000010.0000001)        returntrue;    else        returnfalse;}

The following is the recursive version number of the core's high-speed power:

double PowerWithUnsignedN(doubleunsignedint n){    if (0return1;    if (1return base;    double res = PowerWithUnsignedN(base, n>>1);    res *= res;    if1//n为奇数        res *= base;    return res;}

Under normal circumstances, the above code has been very perfect ~

Just assume you are more efficient, and the recursive version number will not satisfy you. Then you can try the following non-recursive version number:

double PowerWithUnsignedN(doubleunsignedint n){    double1.0;    while0)    {        if1)            res *= base;        base *= base;        1;    }    return res;}

On the high-speed power, we often use to do high-speed power to take the mold, a little more complex, we can use it to do the matrix of high-speed power. The principle is the same, only the object of the operation is a matrix rather than a number (the matrix high-speed power to the Fibonacci sequence is more famous.) This is not a temporary deployment. The Fibonacci sequence will be inscribed in the back. Interested readers are able to find the relevant information on their own first.

Make a little progress every day, Come on!
('? ') )

My level is limited. If there are errors or omissions in the content of the article, please point out to readers. Thank you!

"Daily algorithm" high-speed power

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.