2

I have run into a storage problem. I have a very large "magic" number that I'd like to use frequently (cuts computation time by a staggering 98%). The catch is that the number takes almost 40h to compute, so if I could save it and reload it in a reasonable time, the savings would be fantastic.

Here is the representative code to illustrate the problem.

num = 256...# Very large integer

num.size
=> 13584958492 # bytes

# To not get "string size to large error" when loading.
array_of_bytes = num # broken into sets of 100 MB

File.open("large_num", "a") do |line|
  array_of_bytes.each do |bytes|
    line.puts bytes
  end
end

# To load the number
num = 0

File.open("large_num", "r").each do |line|
  num = (num<<(8*10**8)) | line.to_i
end

# num is not equal to the input number

NOTE: I did add the necessary amount of zeros' by bit shifting num before making the array, and bit shifted them away after loading the number.

So either, if someone knows where it goes wrong and how to fix it. Or if you know a better way of doing it, id be very grateful.

CookieNinja
  • 211
  • 1
  • 4

0 Answers0