1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-14 13:19:42 +01:00
CodeWars/7kyu/rotate_for_a_max/solution.kt
Matej Focko fc899b0b02
chore: initial commit
Signed-off-by: Matej Focko <mfocko@redhat.com>
2021-12-28 16:19:58 +01:00

26 lines
700 B
Kotlin

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
}