507. Perfect Number (Easy)

We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.

Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not.

Example:

Input: 28
Output: True
Explanation: 28 = 1 + 2 + 4 + 7 + 14

Note: The input number n will not exceed 100,000,000. (1e8)

Approach #1 Brute Force [Time Limit Exceeded]

Algorithm

In brute force approach, we consider every possible number to be a divisor of the given number $$num$$, by iterating over all the numbers lesser than $$num$$. Then, we add up all the factors to check if the given number satisfies the Perfect Number property. This approach obviously fails if the number $$num$$ is very large.

Java

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

        }
        return sum == num;
    }
}

Complexity Analysis

  • Time complexity: $$O(n)$$. We iterate over all the numbers lesser thannn.

  • Space complexity: $$O(1)$$. Constant extra space is used.

Approach #2 Optimal Solution [Accepted]

Algorithm

In this method, instead of iterating over all the integers to find the factors of $$num$$, we only iterate upto the $$\sqrt{n}$$. The reasoning behind this can be understood as follows.

Consider the given number $$num$$ which can have $$m$$ distinct factors, namely $$n_1, n_2,..., n_m$$. Now, since the number $$num$$ is divisible by $$n_i$$​​, it is also divisible by $$n_j = num/n_1$$ i.e. $$n_i*n_j=num$$. Also, the largest number in such a pair can only be upto $$\sqrt{num}$$($$\sqrt{num}$$X$$\sqrt{num}=num$$). Thus, we can get a significant reduction in the run-time by iterating only upto $$\sqrt{num}$$ and considering such $$n_i$$'s and $$n_j$$​​'s in a single pass directly.

Further, if $$\sqrt{num}$$ is also a factor, we have to consider the factor only once while checking for the perfect number property.

We sum up all such factors and check if the given number is a Perfect Number or not. Another point to be observed is that while considering 1 as such a factor, $$num$$ will also be considered as the other factor. Thus, we need to subtract $$num$$ from the $$sum$$.

version 1: 6ms

class Solution {
public:
    bool checkPerfectNumber(int num) {
        if (num <= 0) return false;
        int sum = 0;
        for (int i = 1; i*i <= num; ++i) {
            if (num % i == 0) {
                sum += i;
                if (i*i != num) sum += num/i;
            }
        }
        return sum-num == num;
    }
};
class Solution {
public:
    bool checkPerfectNumber(int num) {
        if (num <= 1) return false;
        int sum = 1;
        for (int i = 2; i*i <= num; ++i) {
            if (num % i == 0) {
                sum += i;
                if (i*i != num) sum += num/i;
            }
        }
        return sum == num;
    }
};

Complexity Analysis

  • Time complexity: $$O(\sqrt{n})$$. We iterate only over the range $$1 < i ≤ \sqrt{num}$$.
  • Space complexity: $$O(1)$$. Constant extra space is used.

results matching ""

    No results matching ""