-6

So if the format: {"1","2","3"} was continued until 4,294,967,295 how much RAM would be used?

0x777C
  • 993
  • 7
  • 21
  • How many 32 bit ints are there? Can you not go the mathematics? – Ed Heal Jul 17 '18 at 20:32
  • 2
    What is stopping you from writing a small program to calculate that for yourself? In some systems the amount of memory available won't be enough to hold a lookup table with 2,147,483,647 strings. I suggest a rethink of the policy. – Weather Vane Jul 17 '18 at 20:34
  • Even 2^31 *bytes* occupy 2 gigabytes. So the answer is "too much to be viable". – HolyBlackCat Jul 17 '18 at 20:35
  • 1
    This sure seems like [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What's the point of converting every 32-bit `int` into a string? You expand the storage needed from 4 bytes per `int` value to an average of 9+ bytes per `int` value, **and you get no more information than you had in the first place**. – Andrew Henle Jul 17 '18 at 20:47
  • 1
    Well let's say I had a lookup table initialised on startup and whenever a program wants a conversion it just uses it. Even if that's a stupid idea, I'm still interested in the answer regardless. – 0x777C Jul 17 '18 at 20:53
  • @faissaloo well, you can just guesstimate it. – Martin James Jul 17 '18 at 22:19
  • @faissaloo I think the short answer is, "too much". – Steve Summit Jul 17 '18 at 23:36

2 Answers2

2

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.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

No clue how this is a 'programming' question (nor why its answer is useful), but let's try some math:

it is not worth it creating a table of string pointers, because the pointers themselves are 8 bytes each and the longest string is 10 bytes (NOTE, not adding a 0 terminator to save space!), so it is best to store this as an array of char[10] arrays (i.e. one very long string that has the 1st number at offset 0, the 2nd at offset 10, and so on). Therefore, you need 2^31 * 10 bytes = 21,474,836,480 bytes.

Leo K
  • 5,189
  • 3
  • 12
  • 27