How to Store Extremely Large Numbers

How to store extremely large numbers?

If you already have a boost dependency (which many people these days do), you can use the boost multi-precision library. In fact, it already has an example of a factorial program that can support output up to 128 bits, though extending it further is pretty trivial.

How to store a very large number in c++

You may find your answer here How to store extremely large numbers? GMP answer sounds right, ie this is what it does with pi digits https://gmplib.org/pi-with-gmp.html

How should I store a extremely large Data Type

You can create and edit BigInteger In java, e.g.:

BigInteger bigInteger = new BigInteger("3");
bigInteger = bigInteger.pow(600);
bigInteger = bigInteger.add(new BigInteger("20"));
bigInteger = bigInteger.subtract(new BigInteger("20"));
bigInteger = bigInteger.multiply(new BigInteger("20"));
bigInteger = bigInteger.divide(new BigInteger("20"));

And you can get any part of BigInteger as an Integer, e.g.:

int i = Integer.parseInt(bigInteger.toString().substring(3, 8));

I hope this will help you.

How can I densely store large numbers in a file?

You are trying to store 32 bytes long. Why not just store them as binary numbers? That way you need to store only 32 bytes per number instead of 41 or whatever. You can add on all sorts of quasi-compression schemes to take advantage of things like most of your numbers being shorter than 32 bytes.

If your number is a string, convert it to an int first. Python3 ints are basically infinite precision, so you will not lose any information:

>>> num = '113AB87C877AAE3790'
>>> num = int(num, 16)
>>> num
317825918024297625488

Now you can convert the result to a byte array and write it to a file opened for binary writing:

with open('output.bin', 'wb') as file:
file.write(num.to_bytes(32, byteorder='big'))

The int method to_bytes converts your number to a string of bytes that can be placed in a file. You need to specify the string length and the order. 'big' makes it easier to read a hex dump of the file.

To read the file back and decode it using int.from_bytes in a similar manner:

with open('output.bin', 'rb') as file:
bytes = file.read(32)
num = int.from_bytes(bytes, byteorder='big')

Remember to always include the b in the file mode, or you may run into unexpected problems if you try to read or write data with codes for \n in it.

Both the read and write operation can be looped as a matter of course.

how to handle extremely large numbers

Use a BigInteger

The BigInteger type is an immutable type that represents an arbitrarily large integer whose value in theory has no upper or lower bounds.

Handling very large numbers in Python

Python supports a "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate from the int type, but the interpreter will automatically use whichever is more appropriate. In Python 3.0+, the int type has been dropped completely.

That's just an implementation detail, though — as long as you have version 2.5 or better, just perform standard math operations and any number which exceeds the boundaries of 32-bit math will be automatically (and transparently) converted to a bignum.

You can find all the gory details in PEP 0237.



Related Topics



Leave a reply



Submit