Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a library for HUGE integers [closed]

I am looking for a Java library that can handle truly huge numbers or suggestions as to how to implement this myself. We are talking way beyond BigInteger. How about 2^39614081257132168796771974655+1 for example.

Clearly I could, theoretically, make use of a TreeSet<BigInteger>, one entry per bit and do all the math old-school but I am looking for something that can actually do some real maths with these numbers using built-in math hardware. I don't expect anything truly fast but I would very much like to get close.

It is likely that the number of set bits may be quite small - I am representing G2 polynomials.

Does anyone know of anything out there?

I suspect a feature of the package must be a setBit(BigInteger i).

Added

Thanks for the suggestion of Apfloat. Sadly the following is not possible. It complains that the second parameter must be a long.

    Apint two = new Apint(2);
    Apint big = new Apint("39614081257132168796771974655");
    ApintMath.pow(two, big);

Please note that I am also open to suggestions as to how to do this myself.

Added - to attempt a reopen.

Please see user2246674's post reminding us how staggeringly enormous these numbers are - I can assure you we are not talking about some ordinary math library here, we are talking some serious Math.pow(age-of-the-universe,atoms-in_the_galaxy) kind of numbers - we are certainly not looking for mere opinionated answers.

like image 534
OldCurmudgeon Avatar asked Dec 26 '22 01:12

OldCurmudgeon


1 Answers

This isn't an answer; it is here because I think it's important to realize how insanely big such a number is and why a standard arbitrary precision math library will not work - ever.

The library must have support to directly deal with higher-order equations (such as that which powers Wolfram|Alpha). I believe this is a good question to have on SO specifically because numbers of this magnitude must be treated special.


A standard bit encoding won't work here - if this were possible then BigInteger would also likely suffice (as would Apfloat which was mentioned). The fundamental problem is that 2^39614081257132168796771974655 is huge. Like, really, really big. It only makes sense to deal with numbers of this size using equations!

Let's reason how much a standard one's or two's complement encoding takes by looking at the storage required for several common maximum integer values:

  • 2^8 takes 8 bits; or 1 byte (8/8)
  • 2^32 takes 32 bits; or 4 bytes (32/8)
  • 2^64 takes 64 bits; or 8 bytes (64/8)

Thus, 2^39614081257132168796771974655 requires 39614081257132168796771974655/8 (or ~5x10^27) bytes of memory if using a similar encoding.

A terabyte of memory is only 1x10^12 bytes: it requires more than a QUADRILLION TERABYTES to use a standard encoding on a number of this magnitude.

like image 111
user2246674 Avatar answered Jan 11 '23 20:01

user2246674