Files
  • main.rb
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
f=->x,i=0{k=0;(x<t=1e3)?"CM900D500CD400C100XC90L50XL40X10IX9V5IV4I1".gsub(/(\D+)(\d+)/){v=$2.to_i;s=x/v;x%=v;($1*s).bytes.map{|c|k+=c==73&&i>0?77+175*~-i:c+175*i}}:k=f[x/t,i+1]+f[x%t,i];k}

[
100,        # 67
1,          # 73
4,          # 159
22,         # 322
5000,       # 261
2016,       # 401
1000000000, # 427
1991,       # 449
9999,       # 800
2222222222, # 4688
2147483647, # 5362
].each do |x|
puts f.call x
end
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]