1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-09-19 22:16:57 +02:00
CodeWars/6kyu/playing_with_digits/solution.kt

17 lines
383 B
Kotlin
Raw Normal View History

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
}