Skip to content

Instantly share code, notes, and snippets.

@asethia
Created August 9, 2020 21:41
Show Gist options
  • Save asethia/08b63207151047ab7d65378c5373975d to your computer and use it in GitHub Desktop.
Save asethia/08b63207151047ab7d65378c5373975d to your computer and use it in GitHub Desktop.
Base conversion - like base 10 to 62 or base 10 to 32
/**
* 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