views:

378

answers:

14

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.

+4  A: 

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.

Heath Hunnicutt
I'll try Mathematica presently.
ron8
Are you working with whole numbers, or with numbers which also have a fractional part? In either case, Mathematica will work, I am just curious as to what other solutions apply to you.
Heath Hunnicutt
If you specify a precision, you can't do things like add 2 to a number with 500,000 zeros.. unless you use a bignum module or... Erlang.. which is free. Last I checked, Mathematica was expensive... and was WAY more powerful than just needing (nearly)infinite precision.
snicker
I thought there was a trial... but you must apply for it. I will probebly try Erlang or Python.
ron8
+3  A: 

Perl has a bignum module to do that sort of thing, and Python supports it natively.

lemnar
Python doesn't have a bignum module: its integers are natively unlimited precision.
Ned Batchelder
+10  A: 

I know that Erlang has support for unlimited size int arithmetics.

jldupont
+1 Erlang is very popular for this. And it is free... unlike Mathematica.
snicker
Erlang does not have arbitrary precision floats.
Christian
Further clarification: having browsed the source code of Erlang, the integer arithmetic is written in C so it very much native IMO.
jldupont
+13  A: 

Python and Java have native support, libraries exist for C++, C, .NET, ...

arul
I wouldn't describe Java's BigInteger class as "Native Support".
bendin
I dunno, BigInteger isn't as "native" or as pleasant to use as what Python provides, but at least it comes with the language.
Jason Orendorff
+4  A: 

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.

Mark Rushakoff
Depends on the version of Haskell. Helium, for example, does not support arbitrary-precision arithmetic.
Barry Brown
Good point, Barry. Answer updated.
Mark Rushakoff
+3  A: 

Perl, Python, Ruby, and Java can all do that. External libraries exist for everything else.

I rather like Ruby and Python because they automatically switch from Fixnum to Bignum. (Python: int to long.)

DigitalRoss
Erlang also does this, as do several other programming languages not on your list.
Bob Aman
+1  A: 

In C or C++, you can use GMP (Gnu Multi-Precision library).

In Perl, you can use the bignum module.

Kinopiko
+6  A: 

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.

Omnifarious
+3  A: 

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.

Calyth
+4  A: 

Common Lisp has built-in support for arbitrary large numbers as well...

Nils Pipenbrinck
+1  A: 

MIT/GNU Scheme has support for arbitrarily large numbers.

Jonathan Leffler
+4  A: 

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;-).

Alex Martelli
A: 

Many functional languages natively support arbitrary-precision numbers. Some have already been mentioned here, but I'll repeat them for completeness:

Barry Brown
A: 

I find Python quite good for this.

Alix Axel