A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x
is an integer that can divide x
evenly.
Given an integer n
, return true
if n
is a perfect number, otherwise return false
.
Example 1:
Input: num = 28 Output: true Explanation: 28 = 1 + 2 + 4 + 7 + 14 1, 2, 4, 7, and 14 are all divisors of 28.
Example 2:
Input: num = 7 Output: false
Constraints:
1 <= num <= 108
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The perfect number problem asks us to determine if a number equals the sum of its divisors (excluding the number itself). The brute force method involves checking every possible number that could be a divisor.
Here's how the algorithm would work step-by-step:
def is_perfect_number(number_to_test):
sum_of_divisors = 0
# Iterate through all possible divisors less than the number
for possible_divisor in range(1, number_to_test):
# Check if the possible divisor divides evenly
if number_to_test % possible_divisor == 0:
# Add the divisor to the running sum
sum_of_divisors += possible_divisor
# Determine if the number is perfect
if sum_of_divisors == number_to_test:
return True
return False
The goal is to find if a number equals the sum of its divisors (excluding itself). We can do this more efficiently than checking every number by only checking up to the square root of the given number. We leverage the fact that divisors come in pairs.
Here's how the algorithm would work step-by-step:
def is_perfect_number(number): if number <= 1:
return False
divisor_sum = 1
square_root = int(number**0.5)
# Iterate up to the square root to find divisors
for possible_divisor in range(2, square_root + 1):
if number % possible_divisor == 0:
#Add both divisor and its pair
divisor_sum += possible_divisor + number // possible_divisor
#Correct if it's a perfect square
if possible_divisor == number // possible_divisor:
divisor_sum -= possible_divisor
#Subtract the number itself, as per definition.
divisor_sum -= 0
# Check if the sum of divisors equals the number
return divisor_sum == number
Case | How to Handle |
---|---|
Input number is negative | Return false immediately as perfect numbers are defined as positive integers. |
Input number is zero | Return false immediately as zero is not a perfect number. |
Input number is one | Return false immediately as 1 has no proper divisors, and the sum of its proper divisors is 0. |
Input number is a large number that causes integer overflow when summing divisors | Use a larger integer type (e.g., long) to store the sum of divisors to prevent overflow. |
Input number is a prime number | Return false immediately as a prime number has only 1 as a proper divisor, which will never equal the prime itself (except for 2 which is not prime). |
Large perfect numbers can lead to performance issues due to divisor calculation | Optimize divisor calculation by only iterating up to the square root of the number. |
Perfect number calculation for extremely large numbers | Consider utilizing techniques such as asynchronous processing or multithreading if the algorithm's computation time surpasses the system's limit. |
No perfect number exists within a limited range. | The algorithm correctly returns false for any non-perfect number within the range of possible inputs. |