Created
August 9, 2020 21:41
-
-
Save asethia/08b63207151047ab7d65378c5373975d to your computer and use it in GitHub Desktop.
Base conversion - like base 10 to 62 or base 10 to 32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* convert base 10 to given new base | |
*/ | |
def baseConversion(base: Int, toBeConvert: Int): List[Int] = { | |
def compute(quotient: Int, acc: List[Int]): List[Int] = { | |
if(quotient<=0) { | |
acc.reverse | |
} else { | |
val q = quotient / base | |
val r = quotient % base | |
compute(q, r::acc) | |
} | |
} | |
compute(toBeConvert, List.empty[Int]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment