I want to reduce the number of characters used to represent a very big number.
For example. Take this number 4593638492...3837292
(1000 characters long)
What techniques or tricks can I use to represent this number using fewer characters (say around 50 characters or even less)
Thanks in advance :)
CodePudding user response:
First, determine if there's a simple mathematical expression to give the number. Maybe a factorization. For example, it takes less space to write 2^64*3^40
than 224269343257001716702690972139746492416
.
Otherwise, as Telescope suggested in their comment, use a higher numerical base. In general, writing the integer n in base b takes approximately (log n)/(log b) characters.
Base 36 is a convenient choice, with case-insensitive alphanumeric numerals, which use 64.25% of the space of the base-10 equivalent.
If you want to stick to ASCII digits, try Base 94, using the printable ASCII characters from 33 to 126. Base 94 numerals are 50.68% of the length of the base-ten equivalent.
If you can use Unicode, maybe pick a block of 1000 Kanji characters to use as your "digits". That takes only 1/3 of the space of base-ten. Or if you can find 10,000 usable "digits", you're down to only 1/4 of the space.
CodePudding user response:
You can't. (Unless your numbers were created from a simple rule, such as, say, the factorial of an integer.)
Characters are represented on 8 bits (one byte) and a number of 1000 digits holds 3322 bits of information. At best you can pack as 416 characters. (Or 208 characters of the wide type, but that brings no compression.)
Think of this: with three-digits numbers, you can represent 1000 distinct numbers (from 000 to 999). This does not allow you to encode arbitrary four-digits numbers, as there are 10000 of these. Unless, for some reason, there are less than 1000 four-digits numbers that need to be encoded, and that subset has a computable definition (e.g. only the odd multiples of 7 or only primes).