It seems simple to ask R ^ n ...

xiaoxiao2021-03-06  86

Asking R ^ n where R is a real number 0.0

1 division

A ^ n = (A ^ (N / 2)) ^ 2 N is an odd A ^ n = a * a ^ (n-1) N is an even number example: a normal algorithm: 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 ~~ 8 multiplication time complexity ~~ o (n). Division method: 2 ^ 8 == (A ^ 4) ^ 2 == ((a ^ 2) ^ 2) ^ 2 ~~ 3 The secondary multiplication time complexity ~~ o (log n).

O (log n) is much faster than o (n). :)

Long Square (long n) {return n * n;}

Long Fastexp (long base, long power) {

IF (Power == 0)

Return 1;

Else IF (Power% 2 == 0)

Return Square (Fastexp (Base, Power / 2);

Else

Return Base * (Fastexp (Base, Power-1);

}

2 formula method

A ^ n = exp (log (a) * n) POW (A, N) Code:

#include #include void main () {Printf ("% lf / n", exp (log (8.0) * 1 / 3.0)); Printf ("% lf / n", POW (8.0, 1 / 3.0));

But it seems that this question is not that there is such a number of digits that are probably not ordinary data types, and there is an example of the power of the large number of power to see this. Is it possible to use it before you get there? Of course, there is no ... I don't know how to save memory and CPU when there is this calculation. . . Continued ... PS: If someone knows a simpler method or can solve it with a normal data type, you can solve it. I am very stupid :) Reference: http://www.comp.nus.edu.sg/ ~ Stevenha / Index.html

转载请注明原文地址:https://www.9cbs.com/read-122556.html

New Post(0)