Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find 2 to the power of n . n ranges from 0 to 200

Tags:

java

c++

c

Assume my system as 32 bit machine. Considering this if I use long int for n>63 I will get my value as 0. How to solve it?

like image 448
mousey Avatar asked May 04 '10 05:05

mousey


People also ask

How do you find 2 to the power of n?

Computer science. Two to the exponent of n, written as 2n, is the number of ways the bits in a binary word of length n can be arranged. A word, interpreted as an unsigned integer, can represent values from 0 (000...0002) to 2n − 1 (111... 1112) inclusively.

What is 2 The Power of 0?

So 2 to the zero power is going to be equal to 1. And, actually, any non-zero number to the 0 power is 1 by that same rationale.

How do you calculate 2 to the power of n in C++?

pow() is function to get the power of a number, but we have to use #include<math. h> in c/c++ to use that pow() function. then two numbers are passed. Example – pow(4 , 2); Then we will get the result as 4^2, which is 16.

How do you calculate 2 to the 4th power?

The '4th power' tells you that you'd multiply the number 2 by itself 4 times. In other words, 2 to the 4th power would be the same as 2 x 2 x 2 x 2. Multiply the 2's together and you'll get 16. So, 2 to the 4th power is 16.


1 Answers

double is perfectly capable of storing powers of two up to 1023 exactly. Don't let someone tell you that floating point numbers are somehow always inexact. This is a special case where they aren't!

double x = 1.0;
for (int n = 0; n <= 200; ++n)
{
    printf("2^%d = %.0f\n", n, x);
    x *= 2.0;
}

Some output of the program:

2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
...
2^196 = 100433627766186892221372630771322662657637687111424552206336
2^197 = 200867255532373784442745261542645325315275374222849104412672
2^198 = 401734511064747568885490523085290650630550748445698208825344
2^199 = 803469022129495137770981046170581301261101496891396417650688
2^200 = 1606938044258990275541962092341162602522202993782792835301376
like image 152
fredoverflow Avatar answered Nov 15 '22 14:11

fredoverflow