1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-09 11:09:07 +01:00
CodeWars/7kyu/rotate_for_a_max/solution.kt

27 lines
700 B
Kotlin
Raw Normal View History

package maxrot
fun maxRot(n: Long): Long {
// keep length of n
val length = n.toString().length
var firstDigit = Math.pow(10.toDouble(), (length - 1).toDouble()).toLong()
var number = n // keep modified n
var maxN = n // keep maximum
// rotate left for the length of n
for (x in 0 until length) {
val highest = number.div(firstDigit).rem(10)
val remainder = number.div(firstDigit).div(10).times(firstDigit).times(10)
number = remainder.plus(number.rem(firstDigit).times(10).plus(highest))
firstDigit = firstDigit.div(10)
if (number > maxN) {
maxN = number
}
}
// return maximum
return maxN
}