g0501_0600.s0507_perfect_number.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0501_0600.s0507_perfect_number
// #Easy #Math #2023_01_06_Time_172_ms_(81.82%)_Space_34.1_MB_(18.18%)
class Solution {
fun checkPerfectNumber(num: Int): Boolean {
var s = 1
for (sq in Math.sqrt(num.toDouble()).toInt() downTo 2) {
if (num % sq == 0) {
s += sq + num / sq
}
}
return num != 1 && s == num
}
}