> ## 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.

# Self Dividing Numbers Python Solution

> Tested Python solution for LeetCode 728 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 728, [Easy](/catalog/easy). Topics: [Math](/catalog/topics/math). [View on LeetCode](https://leetcode.com/problems/self-dividing-numbers/description/).

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

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

## Problem

A **self-dividing number** is a number that is divisible by every digit it contains.

* For example, `128` is **a self-dividing number** because `128 % 1 == 0`, `128 % 2 == 0`, and `128 % 8 == 0`.

A **self-dividing number** is not allowed to contain the digit zero.

Given two integers `left` and `right`, return *a list of all the **self-dividing numbers** in the range* `[left, right]` *(both **inclusive**)*.

### Examples

```
Input: left = 1, right = 22
Output: [1,2,3,4,5,6,7,8,9,11,12,15,22]
```

```
Input: left = 47, right = 85
Output: [48,55,66,77]
```

### Constraints

* `1 <= left <= right <= 10^4`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O((right - left + 1) * log10(right))
    # Space: O(1) extra (output excluded)
    def self_dividing_numbers(self, left: int, right: int) -> list[int]:
        def is_self_dividing(num: int) -> bool:
            remaining = num
            while remaining > 0:
                digit = remaining % 10
                if digit == 0 or num % digit != 0:
                    return False
                remaining //= 10
            return True

        return [num for num in range(left, right + 1) if is_self_dividing(num)]
```

## Complexity

| Time | Space |
| - | - |
| O((right - left + 1) \* log10(right)) | O(1) extra (output excluded) |

## Tags


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