4

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.

ron8
  • 243
  • 2
  • 5
  • 13
  • What do you have against scientific notation? – gn22 Oct 31 '09 at 01:50
  • 3
    Pretty much any language would. You may have to write some code, though :-) – ChssPly76 Oct 31 '09 at 01:50
  • 4
    Python does it without any kind of special library at all. Also, the bc program for Unix, which is a full programming language, can also do it without any kind of special library. – Omnifarious Oct 31 '09 at 01:50
  • Surely he means floating point. –  Oct 31 '09 at 01:50
  • Any language will. If not out of the box, you can always write your own handler. –  Oct 31 '09 at 01:51
  • 1
    @unknown: Any turing-complete programming language that accepts textual input and has access to enough memory, anyway. – Artelius Oct 31 '09 at 01:54
  • @Gurdas because scientific notation is not a number. It's a notation format that cannot express a number such as 10^500000 + 2 without **rounding error**. – snicker Oct 31 '09 at 01:59
  • 2
    @snicker: yes it can, it just needs a *lot* of digits. Double-precision floating point numbers (which are just one implementation based on scientific notation) don't have enough digits, by a long way, but there's no inherent limit to scientific notation in general. – Artelius Oct 31 '09 at 02:26

14 Answers14

13

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

arul
  • 13,998
  • 1
  • 57
  • 77
11

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

jldupont
  • 93,734
  • 56
  • 203
  • 318
6

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
  • 54,333
  • 19
  • 131
  • 194
6

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
  • 854,459
  • 170
  • 1,222
  • 1,395
4

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
  • 18,667
  • 3
  • 39
  • 62
  • I'll try Mathematica presently. – ron8 Oct 31 '09 at 01:48
  • 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 Oct 31 '09 at 01:54
  • 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 Oct 31 '09 at 01:55
  • I thought there was a trial... but you must apply for it. I will probebly try Erlang or Python. – ron8 Oct 31 '09 at 02:04
4

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
  • 249,864
  • 45
  • 407
  • 398
4

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

Nils Pipenbrinck
  • 83,631
  • 31
  • 151
  • 221
3

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

lemnar
  • 4,063
  • 1
  • 32
  • 44
3

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
  • 143,651
  • 25
  • 248
  • 329
3

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
  • 1,673
  • 3
  • 16
  • 26
1

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

In Perl, you can use the bignum module.

1

MIT/GNU Scheme has support for arbitrarily large numbers.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

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

Barry Brown
  • 20,233
  • 15
  • 69
  • 105
0

I find Python quite good for this.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500