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


Here is my solution

divisor,dividend = ARGV
products,bringdown,f,r,quotient = [],[],[],[],[]
(1..dividend.length).each {|x|f << dividend.unpack("a#{x}").join}
str = "a#{f.detect{|x| x.to_i >= divisor.to_i}.length}" + "a1"*dividend.length
u = dividend.unpack(str).select{|x| x != ""}.map{|x| x.to_i}
products << u[0] - u[0] % divisor.to_i
r << u[0] % divisor.to_i
bringdown << ((u[0] % divisor.to_i).to_s + u[1].to_s).to_i
quotient << u[0] / divisor.to_i

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

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

fmt2 = divisor.length + 3 + f.detect{|x| x.to_i >= divisor.to_i}.length
(0...u.length).each do |x|
  print "#{products[x].to_s.rjust(fmt2+x)}\n"
  print "#{("-"*divisor.length).rjust(fmt2+x)}\n"
  print "#{r[x].to_s.rjust(fmt2+x)}#{u[x+1]}\n"
end

Harry

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