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
Prime factorization is the process of expressing an integer greater than one as a product of prime numbers. It is a basic number-theory concept, but it is also directly useful in programming for divisibility logic, greatest common divisor calculations, least common multiple calculations, and factor-based optimizations.
Why Prime Factorization Matters
The mathematical reason it matters is the fundamental theorem of arithmetic: every integer greater than one can be written as a product of primes in a way that is unique up to order.
For example:
- '
84 = 2 * 2 * 3 * 7' - '
360 = 2 * 2 * 2 * 3 * 3 * 5'
You can reorder the factors, but you cannot replace them with a different set of prime factors and still get the same number. That uniqueness is what makes factorization useful for reasoning about divisibility and shared structure between integers.
Trial Division Is the Standard Starting Algorithm
For ordinary programming tasks, the standard algorithm is trial division. Repeatedly divide by the smallest factor you can find, beginning with 2, then continue with odd divisors.
This algorithm is simple, correct, and fast enough for many moderate-size integers.
Format the Result Clearly
A factorization is often easier to read in exponent form than as a repeated list.
This prints 2^3 * 3^2 * 5, which is much easier to scan in explanations or logs.
Stop at the Square Root
The main optimization in trial division is to stop checking divisors once d * d > n. At that point, if the remaining n is greater than one, it must itself be prime.
That is why the algorithm does not keep scanning all the way up to the remaining number. This is also why skipping even divisors after handling 2 is a worthwhile improvement.
Without those two ideas, trial division becomes much slower than it needs to be.
Factors Help With GCD and LCM
Prime exponents give a clean way to think about greatest common divisor and least common multiple.
If:
- '
a = 2^3 * 3^2' - '
b = 2^2 * 3^1 * 5^1'
then:
- the GCD uses the minimum exponent of each prime
- the LCM uses the maximum exponent of each prime
This is not how you would always implement GCD in performance-critical code, but it is a good way to understand why the arithmetic works.
Know the Practical Limits
Trial division is fine for small and medium inputs, but it is not a serious strategy for very large composite numbers, especially large semiprimes. For that you need more advanced algorithms such as Pollard rho or even heavier methods used in computational number theory.
That distinction matters because educational examples often make factorization look universally cheap. It is not. For large integers, factorization can be very hard.
Common Pitfalls
The first pitfall is treating 1 as prime or pretending it has a prime factorization. It does not. Another is forgetting to record the leftover n after the divisor loop ends.
Developers also often keep testing divisors long after the square-root stopping condition has made further checks unnecessary.
Finally, do not assume trial division scales to arbitrarily large integers. It is a practical baseline, not a universal large-number factorization method.
Summary
- Prime factorization writes an integer greater than one as a product of primes.
- Trial division is the standard practical starting algorithm.
- Stopping at the square root and skipping even divisors are important optimizations.
- Prime exponents help explain GCD, LCM, and divisibility rules.
- Large-number factorization requires more advanced algorithms than simple trial division.
Related reading
- Print all numbers whose nonzero digits are in ascending order
- Print all permutation in lexicographic order
- Print all unique combination of factors of a given number
- Printing all possible subsets of a list
- Probability and Neural Networks
- Probability distribution in Python
- Probability of collision when using a 32-bit hash
- Probability prediction method of KNeighborsClassifier returns only 0 and 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.