compute 2^1023 = (okay) -> 2^1024 = (problems)

My machine (or, perhaps the Java Math Library) is unable to calculate 2^1024(+), it can however, calculate up to 2^1023.

If I wanted to calculate a higher value, such as 2^1024, then how would I go about doing this? It can be in standard form if required.

I'm happy to implement C++ for this, but would prefer pure Java.

Any ideas? Is there some sort of algorithm I could use to allow larger operations? Could I some how split the calculation into several individual calculations, run them seperately, and then somehow combine the answers?

(I doubt I need to remind you that these are EXTREMELY large numbers; i.e:

   2^1023 = 8.98846567431158e+307

Which is something like:


So, storing the results in a variable isn't my ultimate goal, I just need to output the solutions...

Thanks! :)
LVL 25
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

try this, (save a .htm and open in a browser)

function power2(maxpower){
var pow =new Array()
for (i=0;i<=digits;i++)
for (power=0;power<=maxpower-1;power++)
for (i=0;i<=len;i++)
for (i=0;i<=lentemp-1;i++)
if (pow[i] >= 10000000000000)

if (pow[lentemp] >= 10000000000000)

for (i=0;i<=len;i++)

 if ((i!=0) && (l!=13))
   {for (j=1;j<=13-l;j++)


<p>Enter value of power and click </p>
<form name="f1">
  <p><input type="text" name="power" size="20">
  <input type="button" value="calculate" onclick="power2(1.0*document.f1.power.value)"> <br>
  <textarea name="txt" rows="20" cols="60"></textarea> </p>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GwynforWebCommented: manipulates the digits in blocks of 13, it is really fast. The function power2(exponent) returns 2^exponent
InteractiveMindAuthor Commented:
That's fantastic, Gwyn! Thank you very much! :-)
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Hi!~GwynforWeb, you have done a nice job:)
but I have two problems after viewing your code..
May I know what is the use of
and also
is there any advantage to manipulates in blocks of 13, but not 12 or 14?
GwynforWebCommented: is to do with integer representation of the Javascript interpreter.The larger the chunks the quicker the algorithm but integers   overflow at about 10^15 so I choose 13, since I will be adding values to and multiplying  each chunk this assures no overflow. The 13 value could be optimsed a bit Im not sure,  I was just being safe.

digits=Math.round(4*(maxpower/130)) is an upper bound on the number of chunks I will need for the final answer.
May I know how digits=Math.round(4*(maxpower/130)) get the upper bound on the number of chunks?
2^10 = 1024 < 10^4

so if maxpower=1000

2^1000 = (2^10)^100 < (10^4)^100 =10^(4*100)

ie 2^1000 has less than 400 digits

chunks are of length 13

hence #chunks  < 400/13 = 4*1000/130 = 4*maxpower/130

(not a great upper bound but good enough, it could be improved but this is not a time intensive part of the code)
Oic, thanks for your explaination:)
very nice
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Math / Science

From novice to tech pro — start learning today.