1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-22 16:43:46 +01:00
CodeWars/5kyu/buddy_pairs/solution.kt

16 lines
424 B
Kotlin
Raw Normal View History

fun getSumOfDivisors(n: Long): Long =
(1 until Math.sqrt(n.toDouble()).toLong() + 1).reduce {
acc, e -> acc + if (n.rem(e) == 0.toLong()) e + n.div(e) else 0.toLong()
}
fun buddy(start: Long, limit: Long): String {
for (n in start..limit) {
val m = getSumOfDivisors(n) - 1
if (m > n && getSumOfDivisors(m) - 1 == n) {
return "($n $m)"
}
}
return "Nothing"
}