I would like to take a number like 96, call squareRoot(96)
, and get a return of "4√6".
I have tried many different versions of
but nothing seems to work. It is driving me crazy! I can provide the functions I wrote to find the factors, but I don't think they are particularly complicated.
I found an answer in python here (python, square root simplification function) and translated it into Kotlin:
fun squareRoot(n: Int): Pair<Int, Int> {
var radical = n
var coefficient = 1
for (i in 2..radical) {
if (radical % (i * i) == 0) {
coefficient *= i
radical /= i * i
for (j in 2..radical) {
if (radical % (j * j) == 0) {
coefficient *= j
radical /= j * j
}
}
}
}
return Pair(coefficient, radical)
}