A program that raises a number x to an arbitrary nonnegative integer, n. How can I make a program using this pseudocode that can be used for theoretical, experimental analysis and comparison
1 2 3 4 5 6 7 8 9 10 11 12
Algorithm rpower(int x, int n):
1 if n == 0 return 1
2 elsereturn x*rpower(x, n-1)
Algorithm brpower(int x, int n):
1 if n == 0 return 1
2 if n is odd then
3 y = brpower(x, (n-1)/2)
4 return x*y*y
5 if n is even then
6 y = brpower(x, n/2)
7 return y*y
#include <iostream>
double brpower( double x, unsigned n )
{
if ( n == 0 ) return 1.0;
double y = brpower( x, n / 2 ); // integer division in c++ (but not Python 3)
return ( n % 2 ? x * y * y : y * y );
}
int main()
{
std::cout << brpower( 3.14159, 25 ) << '\n';
}