[Solved] How much RAM would a lookup table for converting positive 32 bit integers to null terminated strings take


Let S(n) be the string for number n.

Consider S(429,496,730) through S(4,294,967,295). By partitioning this into subranges S(429,496,730) through S(999,999,999) and S(1,000,000,000) through S(4,294,967,295), we can see they require (999,999,999−429,496,730+1)•10 bytes and (4,294,967,295−1,000,000,000)•11 bytes (10 bytes for nine digits plus a null terminator, and similarly 11 bytes for 10 digits and a terminator.)

This is 41,949,672,956 bytes.

Consider how to look up any number n in the range 1 to 4,294,967,295. If it is in the range 429,496,730 to 999,999,999, its string starts (n−429,496,730)*10 bytes into the table for the first subrange above. If it is above that, its string starts (n-1,000,000,000)•11 bytes into the second subrange.

If it is less than 429,496,730, we merely add 1,000,000,000 to it and look up S(n+1,000,000,000). The string for S(n) starts at the first non-zero digit after the first byte of S(n+1,000,000,000).

Thus we have proven we need at most 41,949,672,956 bytes to implement a reasonable look-up function that can easily return a pointer to a null-terminated string for any integer from 1 to 4,294,967,295.

Additionally, it is easily seen that no string in the combined table for the two subranges is a substring of any other string, which implies that each string is needed. Therefore, 41,949,672,956 bytes is necessary and sufficient for a function that returns a pointer to prepared strings.

solved How much RAM would a lookup table for converting positive 32 bit integers to null terminated strings take