A program or library for processing massive numbers

I am looking for a way to handle numbers that are in tens of millions of digits, and can do the math at that level. I can use Java and a bit of Python. Thus, a library for one of them will be convenient, but a program will also work that can process such numbers. Anyone have any suggestions?

thanks

+4
source share
5 answers

For Java, take a look at the built-in BigInteger and BigDecimal classes. BigInteger numbers are limited in size by available memory. BigDecimal can represent arbitrarily large base-10 numbers with an accuracy of 2 32 digits to the right of the decimal point (in a practical sense, it is also limited by available memory).

Both of them are limited by rather elementary mathematical operations. (Arithmetic, integers, etc. No roots, logs, etc.). If you need something more, check out the list of libraries on the Java Numerics NIST page. ( Apfloat package supports transcendental functions with arbitrary precision and complex math.)

+5
source

Python can handle large numbers without any libraries.

 >>> 100 ** 100 1000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000L 

Although, as suggested by others, you can use gmpy if you need extra speed.


In Java, you can use BigInteger .

+5
source

For Python, I recommend gmpy , the Python wrapper for the GNU Bignum Library . While Python in theory processes arbitrarily large integers (limited only by memory), and this is normal for numbers of several thousand digits or so, this is not very suitable for working with millions of digits. It does not use modern algorithms for fast multiplication, digit conversion and other standard operations. gmpy , by contrast, is designed to handle numbers of this kind.

The following are some sample timings showing that even for several thousand digits, gmpy significantly faster than Python builtin longs:

 $ python -m timeit -s "from gmpy import mpz" "str(mpz(10)**10000)" 1000 loops, best of 3: 575 usec per loop $ python -m timeit "str(10**10000)" 100 loops, best of 3: 11.8 msec per loop 

Aside: at some point, one of the developers of the Python kernel tried to replace Python long integer with the one that used GMP directly. It turned out that this actually slowed down Python for everyday non-huge whole use cases. See http://bugs.python.org/issue1814 for more details.

+2
source

BigDecimal should be good enough. However you can take a look at jscience

+1
source

What math operations do you need?

Java already provides classes, such as java.math.BigDecimal or java.math.BigInteger, which you can use to create basic material (addition, multiplication, etc.).

0
source

Source: https://habr.com/ru/post/1447768/


All Articles