/*************************************** **************/
/**/
/* Calculate the value of the exponential function (n of */
/* Use the brute force algorithm and divide algorithm */
/* Author: lixiongwei */
/* Time: 06/11/11 sun .*/
/* Win XP + (TC/win_tc/VC + + 6.0 )*/
/**/
/*************************************** **************/
# Include <stdio. h>
# Include <conio. h>
# Include <time. h>
# Include <stdlib. h>
/****************** Function prototype declaration ****************** *****/
Double my_power1 (double A, int N );
Double my_power2 (double A, int N );
Int main ()
{
Int N;
Long int I;
Double result,;
Clock_t start, end;
Double elapsed;
Printf ("Please enter a (unsigned double) and N (unsigned INT):/N ");
Scanf ("% lf % d", & A, & N );
Srand (unsigned) Time (null);/* initialize a random number */
/* Use the brute force algorithm */
Start = clock ();
For (I = 0; I <200000; ++ I)
Result = my_power1 (A, n);/* call the exponential computing function */
End = clock ();
Elapsed = (double) (end-Start)/clk_tck;
Printf ("/n % F to the power of % d is % F/n", A, N, result );
Printf ("my_power1 (% F, % d) * 200000 use time: % fs/N", A, N, elapsed );
/* Use the grouping algorithm */
Start = clock ();
For (I = 0; I <200000; ++ I)
Result = my_power2 (A, n);/* call the exponential computing function */
End = clock ();
Elapsed = (double) (end-Start)/clk_tck;
Printf ("/n % F to the power of % d is % F/n", A, N, result );
Printf ("my_power2 (% F, % d) * 200000 use time: % fs/N", A, N, elapsed );
Getch ();
Return 0;
}
/***************** Use the brute force algorithm to evaluate the function definition of an exponential function ************ *******/
Double my_power1 (double A, int N)
{
Double result = 1;
If (A <= 0) | (n <= 0 ))
{
Printf ("You enter wrong, please again./N ");
Exit (1 );
}
While (n --)
Result * =;
Return result;
}
/***************** Use the grouping algorithm to evaluate the function definition of the exponential function ************ *******/
Double my_power2 (double A, int N)
{
Int X, Y;
If (A <= 0) | (n <= 0 ))
{
Printf ("You enter wrong, please again./N ");
Exit (1 );
}
If (1 = N)
Return;
If (n> 1)
{
While (1)
{
X = rand () % N;
If (X! = 0)
Break;
}/* Randomly determine the two scales in the grouping algorithm */
Y = N-X;
Return my_power2 (A, x) * my_power2 (A, y );
}
}
/*************************************** **************/
/**/
/* When a and n = 1, C (1) = 0 ;*/
/* B. When n> 1, C (n) = C (x) + C (y) + 1 */
/* Where n = x + y ;*/
/* WHEN n = 2 ^ K, C (n) = 2C (n/2) + 1 =... = nlog2 # n-1 */
/*************************************** **************/