Question:
How can we add a number of 200000-digits in C/C++ or Java?
Ankit Verma
2010-09-04 09:10:06 UTC
How can we add a number of 200000-digits in C/C++ or Java?
Five answers:
?
2010-09-05 09:31:59 UTC
+1 to Rachetr for the good answer (java.math.BigInteger for Java, and third-party libraries for C and C++),



I'll just add that a well-known big integer library for C is the GNU MP library (http://www.gmplib.org/) which also has an easy-to-use C++ interface available: http://www.gmplib.org/manual/C_002b_002b-Class-Interface.html#C_002b_002b-Class-Interface

Depending on your development system, you may already have the GNU libraries, see if the following compiles:



#include

#include

#include

int main()

{

     mpz_class n1(std::string(200000, '1')); // 200,000 ones

     mpz_class n2(std::string(200000, '2')); // 200,000 twos

     mpz_class n3 = n1+n2;

     std::cout << n3.get_str(10) << '\n';

}



test run on a typical Linux

~ $ g++ -Wall -Wextra -pedantic -ansi -o test test.cc -lgmpxx

~ $ ./test

3333333333333333333333333333333333333333333333333
Ratchetr
2010-09-04 09:40:54 UTC
Java has built in support for that in java.math.BigInteger.



For C++, there are a few public domain implementations with similar functionality.

Google C++ BigInteger and a few will show up.



There are a few C implementations as well, Google C BigInteger. C would be a clumsy language to use for this, without a string class or operator overloading.



You could of course also write your own. If addition is the only operator you need, you can do it reasonably easily. Store the digits as strings. To do addition, you start with the right most digits and add them as well as carry value. If > 9, you need to mod by 10 and set carry to 1 else carry = 0.

You'll have to deal with the case where the strings are different lengths, and the case where the final result is longer than the original (because of carry). But not all that hard. Fun programming exercise.
?
2010-09-04 10:17:22 UTC
I don't know about java but in c/c++ the range of data can be accepted is:-

data type | byte | bits | Range

short int | 2 | 16 | -32,768 -> +32,767 (16kb)

unsigned short int | 2 | 16 | 0 -> +65,535 (32Kb)

unsigned int | 4 | 16 | 0 -> +4,294,967,295 ( 4Gb)

int | 4 | 32 | -2,147,483,648 -> +2,147,483,647 ( 2Gb)

long int | 4 | 32 | -2,147,483,648 -> +2,147,483,647 ( 2Gb)

signed char | 1 | 8 | -128 -> +127

unsigned char | 1 | 8 | 0 -> +255

float | 4 | 32 |

double | 8 | 64 |

long double | 12 96
Graham B
2010-09-04 10:07:07 UTC
Hi

Python has a technique for handling very large integers.

I just tried 10**200000. It took a couple of seconds but gave me a screen full of, surprise, surprise 200000 zeros !!!

G
Gardner
2010-09-04 09:35:03 UTC
There isn't even an integer field that will hold 200,000 digits for either of those languages.



You'll have to work out some sort of alternative for dealing with numbers of that size.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...