Efficiently getting all divisors of a given number
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
Finding all divisors of a given number efficiently is a classical problem in mathematics and computer science, often needed in algorithms, number theory, cryptography, and optimization problems. While a brute-force approach involves checking each number up to the given number to see if it divides evenly, more efficient methods allow you to determine all divisors in significantly less time, especially for large numbers.
Understanding Divisors
A divisor of a number `n` is an integer `d` such that `n % d = 0`. For example, the divisors of 28 are 1, 2, 4, 7, 14, and 28. Divisors come in pairs that multiply to the original number; hence, when you find one divisor, you automatically obtain another.
Efficient Algorithm to Find Divisors
To find all divisors of a number efficiently, you need to only iterate through the integers up to the square root of the number, `sqrt(n)`. This exploits the property that if `n = k * m` and `k <= sqrt(n)`, then `m >= sqrt(n)`. Once you find a divisor `k`, `n/k` is guaranteed to be the corresponding paired divisor.
Steps of the Algorithm
- Calculate the Square Root: Determine the integer value of the square root of the number. This sets the range for your loop.
- Iterate and Check Divisibility: Loop through all integers from 1 to `sqrt(n)`, checking if each integer is a divisor.
- Record Divisor and its Pair: If `i` is a divisor, then both `i` and `n/i` are divisors of `n`.
Algorithm in Pseudocode
- `sqrt(36) = 6`. We iterate from 1 to 6.
- `1` and `36/1` (36) are divisors.
- `2` and `36/2` (18) are divisors.
- `3` and `36/3` (12) are divisors.
- `4` and `36/4` (9) are divisors.
- `6` is a divisor, and since `6` is `sqrt(n)`, we don't add it twice.
- Number Theory: Analyzing divisors is foundational in number theory problems, aiding in the study of perfect numbers, amicable numbers, and more.
- Cryptography: Efficient divisor calculations are central to factoring-based cryptographic algorithms.
- Algorithms: Useful in optimization problems where divisor properties are exploited.
Related reading
- Efficiently implementing erode/dilate
- Efficiently randomly shuffling the bits of a sequence of words
- Efficiently selecting a set of random elements from a linked list
- Efficiently sorting a numpy array in descending order?
- Efficiently summing log quantities
- Eigenvectors of a large sparse matrix in Tensorflow
- Election Algorithms - A ring algorithm
- Elegant Python code for Integer Partitioning

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.