> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Perfect Number Python Solution with Tests

> Tested Python solution for LeetCode 507 with 23 pytest cases. Generate a practice environment with lcpy.

LeetCode 507, [Easy](/catalog/easy). Topics: [Math](/catalog/topics/math). [View on LeetCode](https://leetcode.com/problems/perfect-number/description/).

Generate this problem as a practice environment: tested reference solution, 23 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 507   # by problem number
lcpy gen -s perfect_number   # by problem name
```

## Problem

A \<a href="[https://en.wikipedia.org/wiki/Perfect\_number](https://en.wikipedia.org/wiki/Perfect_number)" target="\_blank">\<strong>perfect number\</strong>\</a> is a \<strong>positive integer\</strong> that is equal to the sum of its \<strong>positive divisors\</strong>, excluding the number itself. A \<strong>divisor\</strong> of an integer \<code>x\</code> is an integer that can divide \<code>x\</code> evenly.

Given an integer \<code>n\</code>, return \<code>true\</code>\<em> if \</em>\<code>n\</code>\<em> is a perfect number, otherwise return \</em>\<code>false\</code>.

### Examples

```
Input: num = 28
Output: true
Explanation: 28 = 1 + 2 + 4 + 7 + 14
1, 2, 4, 7, and 14 are all divisors of 28.
```

```
Input: num = 7
Output: false
```

### Constraints

* `1 <= num <= 10^8`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/perfect_number/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/perfect_number/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(sqrt(n))
    # Space: O(1)
    def check_perfect_number(self, num: int) -> bool:
        if num <= 1:
            return False
        total = 1
        i = 2
        while i * i <= num:
            if num % i == 0:
                total += i
                paired = num // i
                if paired != i:
                    total += paired
            i += 1
        return total == num
```

## Complexity

| Time | Space |
| - | - |
| O(sqrt(n)) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.