class BaseConverter(val base: Int, val valueList: IntArray){
    init {
        require(base > 1) {"Bases must be at least 2."}
        require(valueList.isNotEmpty()) {"You must supply at least one digit."}
        require(valueList.all { it < base }) {"All digits must be strictly less than the base."}
        require(valueList.all { it > -1 }) {"Digits may not be negative."}
        require(when (valueList.size){
            1 -> true
            else -> valueList.first() != 0
            }) {"Digits may not contain leading zeros."}
        }
    val baseTen: Int by lazy {
        var n = valueList.size
        val b = base
        var s = 0.toDouble()
        for (i in valueList){
            n--
            s += i * Math.pow(b.toDouble(), n.toDouble())
        }
        s.toInt()
    }

    fun convertToBase(newBase: Int): IntArray{
        require(newBase > 1) {"Bases must be at least 2."}
        var n = baseTen
        val m = mutableListOf<Int>()
        val b = newBase

        if (n == 0){
            return intArrayOf(0)
        }

        while (n != 0){
            val d = n/b
            val r = n.rem(b)
            m.add(0, r.toInt())
            n = d
        }

        return m.toIntArray()
    }
}


/*

    1. Let n be the number of digits in the number. For example, 104 has 3 digits, so n=3.
    2. Let b be the base of the number. For example, 104 is decimal so b = 10.
    3. Let s be a running total, initially 0.
    4. For each digit in the number, working left to right do:
       Subtract 1 from n.
       Multiply the digit times bn and add it to s.
    5. When your done with all the digits in the number, its decimal value will be s
 */

/*
    Let n be the decimal number.
    Let m be the number, initially empty, that we are converting to. We'll be composing it right to left.
    Let b be the base of the number we are converting to.
    Repeat until n becomes 0
       Divide n by b, letting the result be d and the remainder be r.
       Write the remainder, r, as the leftmost digit of b.
       Let d be the new value of n.
 */