I have a problem with the statement of a problem, and it gives me a doubt, I am not sure if I am asked for the final result of the power or to print such result as shown in the following format: integerPower( 3, 4 ) = 3 * 3 * 3 * 3.
Thanks for your time. i will be looking forward to seeing your answer.
//Ejercicio 6_18
//Luis Fernando Pinzon
//22 / 02 / 18
/*(Exponentiation) Write a function base
exponent that returns the value of
For example, . Assume that
exponent is a positive, nonzero integer and that base is an integer.
Do not use any math library functions.*/
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int exponentInteger(int, int);
int main(int argc, char** argv)
{
int base;
int exponent;
cout << "Enter a base and a exponent: " << endl;
cin >> base >> exponent;
cout << "\nexponentInteger (" << base << ", " << exponent << ") = " << exponentInteger(base, exponent);
return 0;
}
int exponentInteger(int base, int exponent)
{
int counter = 1;
int potentiation = base;
if(exponent > 0) // verify de exponent to hold the math rule
while(contador < exponent)
{
potentiation *= base; // multiply the base for itself as far as exponent is concerned
counter++;
}
else
{
potentiation = 1;
}
return potentiation;
}
just for fun .. this one does not loop and does minimal multiplications. It cheats out of conditionals as well, by using a lookup table that multiples by 1 when the exponent's current bit is zero and by the correct power when it is 1. It does increasingly less work than a loop for powers above 20 or so. And increasingly more work for small powers. (it does the same amount of work every time, its the loop version that varies).
1 2 3 4 5 6 7 8 9 10 11 12 13 14
longlong ipow(longlong p, unsignedlonglong e) //pow(p,e)
{
constlonglong one = 1;
constlonglong *lut[2] = {&p,&one};
longlong result = 1;
result *= lut[!(e&1)][0]; p *= p;
result *= lut[!(e&2)][0]; p *= p;
result *= lut[!(e&4)][0]; p *= p;
result *= lut[!(e&8)][0]; p *= p;
result *= lut[!(e&16)][0]; p *= p;
result *= lut[!(e&32)][0]; p *= p;
result *= lut[!(e&64)][0];
return result;
}
sure.
3 to the 20th power.
a for loop, to get that, is going to multiply 20 times.
or you could watch how exponents, binary (powers of 2) and all work together.
the lookup table (lut) is really a disguised if-statement. It is the same as saying if the current bit is 1, multiply result by current value of p, else multiply by 1.
20 in binary is 16+4, -> 10100
and we start p at 3 (its the base) and result at 1 (its the answer).
the lowest bit is zero, so result is multiplied by the table[1], which is always the value 1. so result is 1*1 = 1. P is multiplied by 3, so p is now 9.
next line, same thing, next bit, also zero, so p is squared again (81) and result is still 1.
next line, the bit is 1. result is multiplied by p (from its pointer in the lookup table) so result is 81, and p is squared again (6561).
next bit is zero, so p is squared again (43046721)
next bit is 1, so result (81) * 43046721 is your answer. (3486784401).
instead of 20 multiplies, we only did a few, and the larger the exponent, the more it saves. Very small exponents are wasteful, though, as it always does a bit more than 12 operations for every input. If it was 3 to the 1 power, it will still multiply all those lines 6-12... a more practical version would stop at line 9 instead, as that supports up to the 15th power which is good enough for a lot of problems.