首页 > 代码库 > leetcode507

leetcode507

public class Solution {
    public bool CheckPerfectNumber(int num) {
        if (num == 1)
            {
                return false;
            }

            var sum = 1;
            for (int i = 2; i <= num / 2; i++)
            {
                if (num % i == 0)
                {
                    var j = num / i;
                    if (i <= j)
                    {
                        sum += i;
                        sum += num / i;

                        if (sum > num)
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (sum == num)
            {
                return true;
            }
            else
            {
                return false;
            }
    }
}

https://leetcode.com/problems/perfect-number/#/description

leetcode507