Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Hello
Can someone gimme the code to calculate 2^17000(2 to the power 17000),also simultaneously add all the terms.By terms I mean 2^17000 terms.The program should execute within 1 sec.my problem is i am not understanding how to store such a large value.It has to be done only in c++ or java(not in .net or any other platforms) keeping in mind the execution time.
Posted
Updated 19-Aug-13 18:57pm
v2
Comments
CPallini 19-Aug-13 3:56am    
What 'terms'?
[no name] 20-Aug-13 1:26am    
You say "2^17000 terms". That does not make sense. Perhaps you need to give an example of "term".
pasztorpisti 20-Aug-13 8:19am    
Exactly, I was also confused reading that part about the sum. I silently assumed that he has to add exponents of 2.

First of all, what's so big in storage of such big integers. This is a little more than 2K you need for storage, plus some overhear for implementation. Not too big, really. But of course, standard CPU numeric types won't help here.

What you need is called "big integer class". Please see, for example, this open-source implementation: http://sourceforge.net/projects/cpp-bigint/[^].

You can find some more: http://bit.ly/14mQCGc[^].

—SA
 
Share this answer
 
Comments
pasztorpisti 17-Aug-13 8:36am    
5ed, but I would note that calculating a base 2 exponent with computers is also useful, a base 2 exponent is a big byte array with only a single bit set. :-) If OP has to add only 2 exponents that have this bits set on different non-overlapping positions then instead of adding its enough to use binary OR between the arrays...
Sergey Alexandrovich Kryukov 17-Aug-13 11:39am    
Thank you.
—SA
.Net includes a BigInteger structure, which includes a Pow method:
http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.pow.aspx[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Aug-13 1:53am    
OP asks about C++ implementation, not C++/CLI. I answered...
—SA
= 2^17001 - 1
a 2125 byte file with FF
so long for your homework.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900