r/ProgrammerTIL • u/BrainFRZ • Nov 04 '17
Other Language [General] TIL The highest number most programs might handle is about 41373247548 digits.
Almost all programs are written in languages that have a max limit for integers of 263 -1, and we can reach 264 -1 if we make sure it's positive. That's 18446744073709551614, which is 20 digits. Some languages have built-in arbitrary precision number systems that are based on string representations (meaning the number is stored as text). Unfortunately, these languages generally have a string length limit of 231 -1, meaning that's the largest number of digits we can get.(*) That's really cool, and can give us 2147483647 digits.
Then comes GMP. GMP is also useable in most programming languages, and some use them by default, such as Haskell. You can't have a whole number larger than 237 -64 bits (that's over 17GB to hold the one number). So, that value as an actual number is 2137438953408. Unfortunately, I don't have 17GB RAM on my laptop, so I can't calculate it. It's also a little frustrating, because it would only take about 37 calculations to solve, so it'd be done in milliseconds. Fortunately, we have the change of base formula. We can calculate that 2137438953408 is approximately 1041373247548.47235.
Therefore, although I didn't learn what that number was, we know it's about 41373247548 digits. The number of digits is 11 digits long.
(*) Of course every language can do what it wants, but address space to store the list seems to be the general problem. Here's the same problem in Python.
View all comments
Show parent comment
1
u/BrainFRZ Nov 04 '17 edited Nov 04 '17
You're absolutely right that Big Number representations aren't strings, but they're still a class that uses an array to store the digits, much like how a string behaves. So it still has the same immutability and limits on size, which is what I was referring to. Here's the [code in Java] as an example (http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/math/BigInteger.java#BigInteger.0mag).
Also, I was saying about 37 calculations by its Big-O value. O(log 237) = 37. I realize there is a rather large constant factor, but I think it'd take far longer to actually print it than to calculate it. (Also, exponentiation by squaring would be possible here since it's base 2, which is a little faster than modular exponentiation because you don't have any remainders to keep track of.) When I ran it Haskell, it took my laptop 11 seconds before it started printing. Python prints it all at once, so it has to build the representation before we see anything. But yes, "milliseconds" was wrong. Your number is about 80809877 digits, and took about 11 seconds to calculate. Assuming it keeps that rate, then my number would take just over 102 minutes. So I could go watch a movie. :)