Evaluate the mixed radix representation from its most significant end, using Horner's method. There is no need to compute the positional weights separately.
def digsToInt(xs: Array[Int], bs: Array[Int]): Int = {
  require(xs.length == bs.length)
  val N = xs.length
  var i = N
  var acc = 0
  while (i > 0) {
    i -= 1
    acc = xs(i) + bs(i) * acc
  }
  acc
}
The digit conditions are additional preconditions. The loop invariant at the loop test is
An empty sum is zero and an empty product is one, so initialization at is correct. For the next digit, writing gives
which preserves the loop invariant. The loop variant is . At termination , giving
The code executes exactly multiplications, meeting the bound, and uses auxiliary space. When it returns zero. For the time representation in the source, it computes .

Articles by others on the same topic (0)

There are currently no matching articles.