Reputation: 243
What would be the best way to do the following.
Enter a very long number, lets say 500,000 digits long without it going into scientific notation; and then am able to do math with it, like +2 etc.?
Thank you in advance.
EDIT: It is a 500,000 digit, positive integer.
Upvotes: 4
Views: 1899
Reputation: 14094
Python and Java have native support, libraries exist for C++, C, .NET, ...
Upvotes: 13
Reputation: 96834
I know that Erlang has support for unlimited size int arithmetics.
Upvotes: 11
Reputation: 258478
Haskell (when using GHC) also has builtin support for arbitrarily long integers. Here's a snippet showing the length of a number converted to a string.
Prelude> length $ show $ 10
2
Prelude> length $ show $ 1 + 2^2000000
602060
Prelude> let x = 2^200000
Prelude> let y = 2^200000 + 5
Prelude> y - x
5
Or you could just type 2^200000
at the interactive console and wait a couple minutes for it to print out all 600k+ characters. I figured this way was a little simpler to demonstrate.
Upvotes: 4
Reputation: 20624
Many functional languages natively support arbitrary-precision numbers. Some have already been mentioned here, but I'll repeat them for completeness:
Upvotes: 1
Reputation: 882691
Python is pretty good on its own, but better with gmpy
(which bridges it to the GMP library others have mentioned, or alternately to the MPIR kinda-work-alike one [[work in progress;-)]]). Consider:
$ python -mtimeit -s'x=int("1"*9999); y=int("2"*9999)' 'x*y'
100 loops, best of 3: 6.46 msec per loop
i.e., in pure Python, multiply two 10K-digits ints takes 6.5 milliseconds or so. And...:
$ python -mtimeit -s'from gmpy import mpz; x=mpz("1"*9999); y=mpz("2"*9999)' 'x*y'
1000 loops, best of 3: 326 usec per loop
...with gmpy at hand, the operation will be about 20 times faster. If you have hundreds rather than thousands of digits, it's even more extreme:
$ python -mtimeit -s'x=int("1"*199999); y=int("2"*199999)' 'x*y'
10 loops, best of 3: 675 msec per loop
vs
$ python -mtimeit -s'from gmpy import mpz; x=mpz("1"*199999); y=mpz("2"*199999)' 'x*y'
100 loops, best of 3: 17.8 msec per loop
so, with 200k digits instead of just 10k, gmpy's speed advantage is 38 times or so.
If you routinely need to handle integers of this magnitude, Python + gmpy is really a workable solution (of course I'm biased, since I did author and care for gmpy over the last few years exactly because I ♥ Python (hey, my license plate is P♥thon!-) and in one of my hobby (combinatorial arithmetic) I do have to deal with such numbers pretty often;-).
Upvotes: 6
Reputation: 755026
MIT/GNU Scheme has support for arbitrarily large numbers.
Upvotes: 1
Reputation: 86403
Common Lisp has built-in support for arbitrary large numbers as well...
Upvotes: 4
Reputation: 1673
What you're looking for isn't necessarily a language, but an arbitrary-precision library.
GMP would be a fast implementation in C/C++, and scripting languages that handles big integers would probably use something like that.
Upvotes: 3
Reputation: 4053
Perl has a bignum
module to do that sort of thing, and Python supports it natively.
Upvotes: 3
Reputation: 56088
Python does this out of the box with no special library. So does 'bc' (which is a full programming language masquerading as a calculator) for Unix systems.
Upvotes: 6
Reputation:
In C or C++, you can use GMP (Gnu Multi-Precision library).
In Perl, you can use the bignum module.
Upvotes: 1
Reputation: 146231
I rather like Ruby and Python because they automatically switch from Fixnum
to Bignum
. (Python: int
to long
.)
Upvotes: 3
Reputation: 19467
Mathematica allows you to do such math and you can write complete programs in it.
Otherwise, what you seek is a "library" to extend the built-in functionality of another programming language, such as Python or Java.
In the case of Python, the decimal module enables you to specify a precision in which math operations will be peformed.
Upvotes: 4