1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-23 17:03:48 +01:00
CodeWars/6kyu/playing_with_digits/solution.kt
Matej Focko fc899b0b02
chore: initial commit
Signed-off-by: Matej Focko <mfocko@redhat.com>
2021-12-28 16:19:58 +01:00

16 lines
383 B
Kotlin

fun getSum(n: Int, p: Int, l: Int): Int {
var result = 0
var runningN = n
for (q in p + l - 1 downTo p) {
result += Math.pow(runningN.rem(10).toDouble(), q.toDouble()).toInt()
runningN /= 10
}
return result
}
fun digPow(n: Int, p: Int): Int {
val sum = getSum(n, p, n.toString().length)
return if (sum.rem(n) == 0) sum.div(n) else -1
}