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

15 lines
424 B
Kotlin

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"
}