I would like to program a procedure to convert a number in base 10 to a set of digits in an arbitrary base.
Through empirical testing I see that the procedure involves repeated mod operations and div operations to give the digits in least significant order. i.e.
// Returns list of NUMBER's (>=0) digits in BASE (>=2) in least // significant to most significant order. function convertToBase(number, base): digits = list() while number != 0: add (number mod base) to digits let number := number / base return digits
But I have little intuition for why this procedure works? Can someone explain?