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

# Factor Combinations Python Solution with Tests

> Tested Python solution for LeetCode 254 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 254, [Medium](/catalog/medium). Topics: [Backtracking](/catalog/topics/backtracking). [View on LeetCode](https://leetcode.com/problems/factor-combinations/description/).

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

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

## Problem

Numbers can be regarded as the product of their factors.

* For example, `8 = 2 x 2 x 2 = 2 x 4`.

Given an integer `n`, return *all possible combinations of its factors*. You may return the answer in **any order**.

**Note** that the factors should be in the range `[2, n - 1]`.

### Examples

```
Input: n = 1
Output: []
```

```
Input: n = 12
Output: [[2,6],[3,4],[2,2,3]]
```

```
Input: n = 37
Output: []
```

### Constraints

* `1 <= n <= 10^7`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(sqrt(n)^d) where d is the max factorization depth
    # Space: O(d)
    def get_factors(self, n: int) -> list[list[int]]:
        results: list[list[int]] = []
        path: list[int] = []

        def dfs(remaining: int, start: int) -> None:
            if path:
                results.append([*path, remaining])
            for factor in range(start, int(remaining**0.5) + 1):
                if remaining % factor == 0:
                    path.append(factor)
                    dfs(remaining // factor, factor)
                    path.pop()

        dfs(n, 2)
        return results
```

## Complexity

| Time | Space |
| - | - |
| O(sqrt(n)^d) where d is the max factorization depth | O(d) |

## Tags

[NeetCode All](/catalog/neetcode).


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