简体   繁体   中英

How to calculate with googol or even larger numbers in java?

Is that theoretically possible? Even BigIntegers should fail, AFAIK, since they are internally based on an Array, and these have a limited size. Is it possible to directly calculate with the exponential form of googolplexes in Java? for example: ((10^10^100)/2)-5

How do I prepare an application, which has googol-sized numbers as genuine results?

(For those who don't know: a googol == 10^100 , a googolplex == 10^10^100 )

I think you should try symbolic math library. Symbolic math library perform operations on equation structure and do not try to represent numbers internally.

Some links to start:

https://code.google.com/p/symja/

http://www4.ncsu.edu/~kaltofen/bibliography/99/BCK99.pdf

Good luck.

Think of it as how you would calculate scientific notation calculations: For example 2.574e100 * 4.762e15724 can be calculated as (2.574 * 4.762)e(100*15724), rather than calculating 2.574e100 and 4.762e15724 separately, then multiplying them together.

So theoretically possible, and not too difficult to implement. Googol can be represented by two integers, 10 and 100, just like you did in your question when you said "a googol == 10^100".

A more simple explanation of what I said above is this: You wouldn't write googol as 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, you would write it as 10^100. Make the computer do the same.

Why not use BigInteger ? this works:

String googolString = "1";
for (int i = 0; i < 100; ++i) {
    googolString += "0";
}
BigInteger googol = new BigInteger(googolString);

String googolPlexString = "1";
BigInteger googolPlex;

for (int i = 0; i < googolString.length() - 1; ++i) {
    for (int j = 0; j < 100; ++j) {
        googolPlexString += "0";
    }
}

googolPlex = new BigInteger(googolPlexString);

googolPlex = googolPlex.divide(new BigInteger("2"));
googolPlex = googolPlex.subtract(new BigInteger("5"));

But yes storing a number, not a symbol of the number, but the binary representation of a number would be impossible for numbers large enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM