Calculate the value of the exponential function (n of)

Source: Internet
Author: User

/*************************************** **************/
/**/
/* 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 */
/*************************************** **************/

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.