Generating all factors of a number given its prime factorization
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Once you already know a number's prime factorization, generating all of its factors becomes a combinatorics problem rather than a factoring problem. Every divisor is formed by choosing an exponent for each prime between zero and that prime's maximum exponent in the factorization.
Turn the factorization into exponent choices
Suppose:
N = p1^a1 * p2^a2 * ... * pk^ak
Then every divisor of N has the form:
p1^b1 * p2^b2 * ... * pk^bk
where each bi can be any integer from 0 through ai.
For example, if:
- '
60 = 2^2 * 3^1 * 5^1'
then the allowed exponent choices are:
- for
2:0, 1, 2 - for
3:0, 1 - for
5:0, 1
Each combination of those exponent choices gives exactly one factor.
Build factors recursively
A clean implementation is to walk through the prime factors one by one and multiply the current partial product by every allowed power of the current prime.
This prints:
The recursive structure mirrors the mathematical definition directly.
An iterative version works too
If you prefer iteration, start with the trivial factor 1, then expand the list one prime at a time.
This is often easier to read if you want to avoid recursion or you plan to integrate the logic into a larger loop.
Know how many divisors to expect
The factorization also tells you the divisor count immediately. If:
N = p1^a1 * p2^a2 * ... * pk^ak
then the number of divisors is:
(a1 + 1) * (a2 + 1) * ... * (ak + 1)
For 60 = 2^2 * 3^1 * 5^1, the count is:
(2 + 1) * (1 + 1) * (1 + 1) = 12
That matches the generated list exactly. This formula is useful as a correctness check when writing code.
Common Pitfalls
- Forgetting that the exponent range starts at zero, which would omit the divisor
1and many other valid factors. - Confusing prime factors themselves with all divisors generated from exponent combinations.
- Generating the divisors correctly but forgetting to sort them before presenting the result.
- Assuming the task is to factor the number when the prime factorization is already given.
- Ignoring the divisor-count formula and missing an easy correctness check.
Summary
- Once the prime factorization is known, every divisor comes from choosing allowed exponents for each prime.
- A recursive or iterative Cartesian-product style algorithm generates all factors cleanly.
- For
60 = 2^2 * 3^1 * 5^1, the generated factors are1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60. - The number of divisors is the product of
(exponent + 1)for each prime. - This is a divisor-generation problem, not a factorization problem.
Related reading
- Generating all permutations excluding cyclic rotations
- Generating all permutations of a given string
- Generating all permutations of a given string
- Generating circular shifts / reduced Latin Squares in Python
- Generating permutations lazily
- Generating permutations of a set most efficiently
- Generating combinations in C
- Generating m distinct random numbers in the range 0..n-1

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.