Last active
August 12, 2024 21:14
-
-
Save leonid-shevtsov/59248e733f6237577df89c549786b149 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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