How to raise 2 to the power i. Forbidden to use pow
Answer 1, authority 100%
I think if the author of the question meant the integer i
, and not too large, then this is usually done in one operation
result = 1 << i;
However, here you need to do a lot of all kinds of checks, depending on the specific task. For example, if the expected result is greater than the bit depth of the typical data types of the programming language allows. If we are talking about large i
, then long arithmetic should be used.
If i
is fractional, then traditionally several techniques are used for this: reduction of the argument and the subsequent Newton method. There are other options, but it seems to me that the author of the question does not need this.
Answer 2
#include<iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "rus");
int x = 2; //Number
int y = 4; //Degree
int out = x;
for (int i = 1; i < y; i++)
{
out = out * x;
}
cout <<"ANSWER: " << out << endl;
system("pause");
}