当前位置:网站首页>Leetcode perfect number simple

Leetcode perfect number simple

2022-06-13 05:49:00 AnWenRen

title :507 Perfect number - Simple

subject

For one Positive integer , If it and all but itself Positive factor The sum is equal , Let's call it alpha 「 Perfect number 」.

Given a Integers n, If it's a perfect number , return true, Otherwise return to false

Example 1

 Input :num = 28
 Output :true
 explain :28 = 1 + 2 + 4 + 7 + 14
1, 2, 4, 7,  and  14  yes  28  All positive factors of .

Example 2

 Input :num = 6
 Output :true

Example 3

 Input :num = 496
 Output :true

Example 4

 Input :num = 8128
 Output :true

Example 5

 Input :num = 2
 Output :false

Tips

  • 1 <= num <= 108

Code Java

public boolean checkPerfectNumber(int num) {
    
    if (num == 1) return false;
    int sum = 1;
    for (int i = 2; i < Math.sqrt(num); i++) {
    
        if (num % i == 0)
            sum += i + num / i;
    }
    if (sum == num)
        return true;
    return false;
}
原网站

版权声明
本文为[AnWenRen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280507175161.html