One line by Java 8 Stream API


  • 0
    X
    public boolean checkPerfectNumber(int num) {
        return num == 1 ? false : IntStream.rangeClosed(2, (int) Math.sqrt(num))
                                           .filter(i -> num % i == 0).reduce(1, (i, j) -> i + j + num / j) == num;
    }
    

Log in to reply
 

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