Skip to content

Instantly share code, notes, and snippets.

@leonid-shevtsov
Last active August 12, 2024 21:14
Show Gist options
  • Save leonid-shevtsov/59248e733f6237577df89c549786b149 to your computer and use it in GitHub Desktop.
Save leonid-shevtsov/59248e733f6237577df89c549786b149 to your computer and use it in GitHub Desktop.
max_precise = 10 ** Float::DIG
last_best = 10
2.upto(256) do |base|
total = base
bits = 0
power2 = 1
chars = 1
results = []
loop do
while power2*2 <= total
power2 *= 2
bits += 1
end
if bits >= 8
results << {chars:,bytes: bits/8, bits:, waste: chars/(bits/8).to_f}
end
break if total*base > max_precise
total = total * base
chars += 1
end
optimal = results.min_by{|r| r[:waste]}
if optimal[:waste] < last_best
puts "\"#{"% 7s" % "Base#{base}"}: #{"%2i" % optimal[:bytes]} => #{"%2d" % optimal[:chars]}\",#{optimal[:waste]}"
last_best = optimal[:waste]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment