     step null, a, b
 
   times__: (x) -> new LongInt @sign * x.sign, mul @digits, x.digits
+
+  divmod = (r, s) ->
+    scale = Math.floor BASE / (s.last() + 1)
+    [r_, s_] = (seq seqTimesDigit(x, scale) for x in [r, s])
+    [m, d] = [s_.size(), s_.last() + 1]
+
+    step = (q, h, t) ->
+      f = if h?.size() < m
+        0
+      else
+        n = (h?.last() * if h?.size() > m then BASE else 1) or 0
+        (Math.floor n / d) or (if cmp(h, s_) >= 0 then 1 else 0)
+
+      if f
+        -> step(add(q, seq.conj(f)), sub(h, seqTimesDigit(s_, f)), t)
+      else if t
+        -> step(seq.conj(0, -> q), seq.conj(t.first(), -> h), t.rest())
+      else
+        [cleanup(q), h && div(h, seq [scale])]
+
+    trampoline step null, null, r_.reverse()
+
+  div = (r, s) -> divmod(r, s)[0]
+
+  mod = (r, s) -> divmod(r, s)[1]
+
+  div__: (x) ->
+    d = cmp @digits, x.digits
+    if d < 0
+      asNum 0
+    else if d == 0
+      asNum @sign * x.sign
+    else
+      new LongInt @sign * x.sign, div @digits, x.digits
+
+  mod__: (x) ->
+    d = cmp @digits, x.digits
+    if d < 0
+      this
+    else if d == 0
+      asNum 0
+    else
+      new LongInt @sign * x.sign, mod @digits, x.digits
 
   zeroes = BASE.toString()[1..]
 

        seq.conj(digit, -> step(r_.rest(), s_.rest(), borrow))
      else
        s or r
    step r, s

  minus__: (x) ->
    if @sign != x.sign

  ONE  = seq [1]
  TWO  = seq [2]

  cleanup = (s) -> s?.reverse()?.dropWhile((x) -> x == 0)?.reverse() or null

  add = (r, s, c = 0) ->
    if c or (r and s)
      [r_, s_] = [r or ZERO, s or ZERO]
