Number Theory
Prime Factorization
Factors
Mathematics
Algorithms

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.

Practice algorithms

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.

python
1def generate_factors(factors, index=0, current=1, result=None):
2    if result is None:
3        result = []
4
5    if index == len(factors):
6        result.append(current)
7        return result
8
9    prime, exponent = factors[index]
10    value = 1
11    for _ in range(exponent + 1):
12        generate_factors(factors, index + 1, current * value, result)
13        value *= prime
14
15    return result
16
17
18prime_factorization = [(2, 2), (3, 1), (5, 1)]
19all_factors = sorted(generate_factors(prime_factorization))
20print(all_factors)

This prints:

python
[1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60]

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.

python
1def generate_factors_iterative(factors):
2    divisors = [1]
3
4    for prime, exponent in factors:
5        new_divisors = []
6        power = 1
7        for _ in range(exponent + 1):
8            for d in divisors:
9                new_divisors.append(d * power)
10            power *= prime
11        divisors = new_divisors
12
13    return sorted(divisors)
14
15
16print(generate_factors_iterative([(2, 2), (3, 1), (5, 1)]))

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 1 and 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 are 1, 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.