DEV Community

Discussion on: Daily Challenge #287 - 16+18=214

Collapse
 
n8chz profile image
Lorraine Lee

Ruby, because Ruby has built-in divmod, because you need both at least as often as you need either.

def piecewise_add(n1, n2)
  return n1+n2 if n1*n2 == 0
  d1, m1 = n1.divmod(10)
  d2, m2 = n2.divmod(10)
  m3 = m1+m2
  mult = m3 > 9 ? 100 : 10
  mult*piecewise_add(d1, d2)+m3
end