Bases

avatar
(Edited)

class AllYourBase {

int number = 0; 



AllYourBase(inputBase, digits) {

    if(inputBase < 2) throw new ArithmeticException()

    int exp = digits.size;

    for(int i=0; i< digits.size; i++){

        if(digits[i] < 0 || digits[i] >= inputBase) throw new ArithmeticException()

        number += Math.pow(inputBase, --exp)*digits[i]

    }

}

def rebase(outputBase) {

    int toAdd = 0

    var list = []

 

    if(outputBase < 2) throw new ArithmeticException()

    if(number == 0) return [0]

    while(number > 0){

        toAdd = number % outputBase

        list.add(0,toAdd)

        number /= outputBase

    }

    return list;

}


0
0
0.000
0 comments