>
> ## Long Division (#180)
>
> Your task this week is to perform and display long division.
>

This is about the same as my first solution.
I just cleaned up the code a little.

divisor,dvd = ARGV[0].to_i,ARGV[1]
f, quotient, r, products = [],[""],[""],[""]
(1..dvd.length).each {|x|f << dvd.unpack("a#{x}").join}
g = f.detect{|x| x.to_i >= divisor}.length
str = "a#{g}" + "a1"*(dvd.length-g)
dividend = dvd.unpack(str).unshift("")

(0...dividend.length-1).each do |x|
  quotient << (r[x] + dividend[x+1]).to_i / divisor
  r << ((r[x] + dividend[x+1]).to_i % divisor).to_s
  products << (r[x] + dividend[x+1]).to_i - r[x+1].to_i
end

fmt = dvd.length + 3 + divisor.to_s.length
print "#{quotient.join.rjust(fmt)}"
print " R#{r[-1]}\n" if r[-1].to_i != 0
print "\n" if r[-1].to_i == 0
print "#{("-" * (dvd.length + 1)).rjust(fmt)}\n"
print "#{divisor.to_s} | #{dvd}\n"

fmt2 = divisor.to_s.length + 3 + g
(1...dividend.length).each do |x|
  print "#{products[x].to_s.rjust(fmt2+x-1)}\n"
  print "#{("-"*(divisor.to_s.length+1)).rjust(fmt2+x-1)}\n"
  print "#{r[x].rjust(fmt2+x-1)}#{dividend[x+1]}\n"
end

Harry

-- 
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby/list.html