JAVA Easy Solution no using sqrt


  • 0
    J
    public class Solution {
        public boolean checkPerfectNumber(int num) {
            if (num <= 0) return false;
            int sum = 0, val = num;
            for (int i = 1; i < val; i++) {
                if (num % i == 0) {
                    if (i == 1) sum += i;
                    else {
                        sum += i;
                        sum += num /i;
                    }
                    val = num /i;
                }
            }
            
            return sum == num;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.