isPerfectNumber

Medium · Number Theory · 4 test cases

A perfect number is a positive integer that is equal to the sum of its proper positive divisors (the sum of its positive divisors excluding the number itself). Write a function to check if a number is a perfect number.

Examples

isPerfectNumber(6) → true
isPerfectNumber(28) → true
isPerfectNumber(12) → false

Starter code

function isPerfectNumber(num) {

}

Complexity of the optimal solution

Time O(sqrt(n)), space O(1). The function finds all divisors by iterating up to the square root of the input number 'n'. This is an efficient way to sum divisors, resulting in O(sqrt(n)) time complexity. Space is constant.

Solve isPerfectNumber in the browser