Division is the hardest operation, so we start with the easy half - dividing a whole magnitude by one small limb. Today you build short division, walking the limbs from the top with a running remainder.
Divide a magnitude by a single limb, producing a quotient and a remainder, top limb first.
Short division is long division you already do by hand: start at the most
significant limb, and at each step form a two-part number from the leftover
remainder and the current limb, divide it by the small divisor to get this
quotient limb, and carry the new remainder down to the next limb. Because the
remainder is always below the divisor (below 10^9), remainder * Base + limb
stays under 10^18 + 10^9, safely inside a 64-bit accumulator.
Note we walk the limbs from the top down, the opposite direction from addition
and multiplication, because in division the high-order digits are resolved first.
This handles any single-limb divisor exactly, which is enough to give the general
DivMod its easy path next lesson - and to power hexadecimal output later, which is
nothing but repeated division by sixteen.
func divScalar(a mag, s uint32) (q mag, r uint32) {q = make(mag, len(a))var rem uint64for i := len(a) - 1; i >= 0; i-- { // top limb firstcur := rem*Base + uint64(a[i]) // fits in 64 bitsq[i] = uint32(cur / uint64(s))rem = cur % uint64(s)}return q.normalize(), uint32(rem)}