List All Factorizations of an Integer into Factors Greater Than 1

Quick Overview

Return every way to write an integer as a product of factors greater than 1 and smaller than the number itself, each list in non-decreasing order and the lists sorted. Tests backtracking with a minimum-factor bound, pruning at the square root and avoiding duplicate orderings.

List All Factorizations of an Integer into Factors Greater Than 1

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Numbers can be regarded as the product of their factors. For example, `8 = 2 x 2 x 2 = 2 x 4`. Write a function that takes an integer `n` and returns all possible combinations of its factors, where every factor is in the range `[2, n - 1]`. ### Function Signature ```python def get_factors(n: int) -> list[list[int]]: ``` ### Rules - Each combination lists its factors in non-decreasing order. `[2, 6]` and `[6, 2]` are the same combination, and only `[2, 6]` appears in the output. - Because every factor is at most `n - 1`, a combination always has at least two factors. `[n]` on its own is not a combination. - Return the combinations sorted in ascending lexicographic order, comparing element by element (the order Python's `sorted` gives for lists of integers). - When `n` has no such combination (`n` is 1 or a prime), return an empty list. ### Constraints - `1 <= n <= 10^7` ### Examples ```text Input: n = 12 Output: [[2, 2, 3], [2, 6], [3, 4]] ``` 12 can be written as `2 x 2 x 3`, `2 x 6` and `3 x 4`, and no other non-decreasing product of factors from 2 to 11 equals 12. `[2, 2, 3]` comes before `[2, 6]` because their second elements compare 2 < 6. ```text Input: n = 32 Output: [[2, 2, 2, 2, 2], [2, 2, 2, 4], [2, 2, 8], [2, 4, 4], [2, 16], [4, 8]] ``` 32 is `2^5`, so each combination matches one way to split the exponent 5 into at least two positive parts: 1+1+1+1+1, 1+1+1+2, 1+1+3, 1+2+2, 1+4 and 2+3. Those six splits give the six lists above, shown in lexicographic order. ```text Input: n = 37 Output: [] ``` 37 is prime, so its only factors are 1 and 37, and neither lies in the range 2 to 36.

Overview: Return every way to write an integer as a product of factors greater than 1 and smaller than the number itself, each list in non-decreasing order and the lists sorted. Tests backtracking with a minimum-factor bound, pruning at the square root and avoiding duplicate orderings.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Numbers can be regarded as the product of their factors. For example, 8 = 2 x 2 x 2 = 2 x 4.

Write a function that takes an integer n and returns all possible combinations of its factors, where every factor is in the range [2, n - 1].

Function Signature

def get_factors(n: int) -> list[list[int]]:

Rules

  • Each combination lists its factors in non-decreasing order. [2, 6] and [6, 2] are the same combination, and only [2, 6] appears in the output.
  • Because every factor is at most n - 1 , a combination always has at least two factors. [n] on its own is not a combination.
  • Return the combinations sorted in ascending lexicographic order, comparing element by element (the order Python's sorted gives for lists of integers).
  • When n has no such combination ( n is 1 or a prime), return an empty list.

Constraints

  • 1 <= n <= 10^7

Examples

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

12 can be written as 2 x 2 x 3, 2 x 6 and 3 x 4, and no other non-decreasing product of factors from 2 to 11 equals 12. [2, 2, 3] comes before [2, 6] because their second elements compare 2 < 6.

Input: n = 32
Output: [[2, 2, 2, 2, 2], [2, 2, 2, 4], [2, 2, 8], [2, 4, 4], [2, 16], [4, 8]]

32 is 2^5, so each combination matches one way to split the exponent 5 into at least two positive parts: 1+1+1+1+1, 1+1+1+2, 1+1+3, 1+2+2, 1+4 and 2+3. Those six splits give the six lists above, shown in lexicographic order.

Input: n = 37
Output: []

37 is prime, so its only factors are 1 and 37, and neither lies in the range 2 to 36.