How to calculate the integer powers in c [duplicate]
Possible Duplicate:
storing more than 2 power 31 on a 32-bit system
How to calculate 2 to the power of 2010. The no of digits will be more than 32 bits w开发者_Go百科hich is exceeding the integer size to store. Without using arrays how to store it.Please help me undrstand about this.
22010 is bigger than the amount of atoms in our universe[1].
You'll need a library like GMP and 252 bytes to store it.
[1]. Matthew Champion, "Re: How many atoms make up the universe?", 1998
You're going to have to store it in arrays, unless you're using an architecture with 2^2010 bits.
Powers of 2 in binary are simply a 1 followed by the exponent in 0. 2^2010 would be a one with 2010 zeros. Allocate an array to fit everything:
(2010+1) / (sizeof(unsigned int) * 8) //size of array
Then store:
(1 << (2010 % (sizeof(unsigned int) * 8))
In the last element.
It it's a trick question then store it in a single int and say it's little endian.
If you need to store 2 to 2010th power without using arrays, you can do this with a floating-point number, in a format that allows an exponent that large. Of the IEEE-754 types, this would require a quadruple-precision float; since that's likely not available, you might have to roll your own.
However, it sounds like your assignment is about what you know about binary numbers and whether you can come up with a non-obvious solution to a problem rather than about actually storing large numbers. If you just need to print the result, and (as noted in a comment) find the sum of the bits, you don't need to bother with all that.
First, remember what a bit is -- a binary digit. Each bit is one digit of a binary number, i.e. a base-2 number. With numbers in general (with place values, as we're used to), each digit represents a multiple of a certain power of the base that corresponds to its position. So in decimal (base 10) 234
, you have (4 * 10^0) + (3 * 10^1) + (2 * 10^2)
. In binary, 1010
is (0 * 2^0) + (1 * 2^1) + (0 * 2^2) + (1 * 2^3)
. So, if each digit (or bit) corresponds to a power of two, how many bits would be 1
for 2^2010
? If the powers of two corresponding to each bit match the number of the bit position (counting left, starting with zero), which bit(s) would be 1
?
As for displaying the number, it's easy in binary... you should know from the above which bit(s) are 1
, and the rest will be 0
so you can just print them off starting with the largest. You don't have to store the number 2^2010 at all, since you can tell how many bits you'll need and whether any given bit is 1
. You can just count down the bit positions, and display 1
or 0
for each until you're done.
It's also easy in hexadecimal or octal, because the bases of those number systems are powers of two; this has the effect of making each digit in those systems correspond exactly to a certain sized group of digits in base-2. So you just need to figure out how the bits line up with the hexadecimal digits, convert each group of bits to a digit, and print them off.
Use logarithms to calculate, so 2^2010
will be about 1.1756858319608366328144211980059e+605
in the scientific notation.Which can be stored or in the later case its not a practical way!
精彩评论