You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
735 B
C++

Fig02_11.cpp - Recursive exponentiation algorithm, with a test program
#include <iostream.h>
bool isEven( int n )
{
return n % 2 == 0;
}
/* START: Fig02_11.txt*/
long pow( long x, int n )
{
/* 1*/ if( n == 0 )
/* 2*/ return 1;
/* 3*/ if( n == 1 )
/* 4*/ return x;
/* 5*/ if( isEven( n ) )
/* 6*/ return pow( x * x, n / 2 );
else
/* 7*/ return pow( x * x, n / 2 ) * x;
}
/* END */
// Test program
int main( )
{
cout << "2^21 = " << pow( 2, 21 ) << endl;
cout << "2^30 = " << pow( 2, 30 ) << endl;
return 0;
}