def to_base(base, num):
    """Encode a positive integer as a big-endian list of digits in the given
    base.
    """
    if num < 0:
        raise ValueError('only works on positive integers')
    out = []
    while num > 0:
        out.insert(0, num % base)
        num //= base
    return out
