Divisors Finding Algorithm (Optimal)
Divisors Finding Algorithm (Optimal)
Overview
This algorithm efficiently finds all the divisors of a given integer n
. The divisors are the positive integers that divide n
without leaving a remainder. The implementation is provided in C++ using a vector to store the divisors.
Algorithm Description
The algorithm follows these steps:
- Initialize an empty vector
divisors
to store the divisors.
- Note we can also iterate till end but it will increse time complexity
Iterate from
1
to the square root ofn
:Check if
i
is a divisor ofn
by using the modulo (%
) operator.If
i
is a divisor, add it to the vectordivisors
.Additionally, if
i
is not equal ton / i
, addn / i
to the vector to cover both divisors.
Sort the vector
divisors
in ascending order to present the divisors in a structured manner.Print or use the divisors as needed.
Return the vector
divisors
.
Code Implementation
The algorithm is implemented in C++ as follows:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> printDivisorsOptimal(int n) {
vector<int> divisors;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
divisors.push_back(i);
if (i != n / i) divisors.push_back(n / i);
}
}
sort(divisors.begin(), divisors.end());
for (int divisor : divisors) {
cout << divisor << " ";
}
return divisors;
}
Example
Iterate from
1
to the square root ofn
(i = 1 to 3):Iteration 1:
Check if 1 is a divisor of 12 (yes). Add 1 to
divisors
.Check if 1 is not equal to
12 / 1
(no).
Iteration 2:
Check if 2 is a divisor of 12 (yes). Add 2 to
divisors
.Check if 2 is not equal to
12 / 2
(yes). Add12 / 2 = 6
todivisors
.
Iteration 3:
Check if 3 is a divisor of 12 (yes). Add 3 to
divisors
.Check if 3 is not equal to
12 / 3
(yes). Add12 / 3 = 4
todivisors
.
Sort the vector
divisors
in ascending order:divisors
now contains [1, 2, 3, 4, 6, 12].
Print the divisors:
- Output:
1 2 3 4 6 12
- Output:
Output
The divisors of 12 are [1, 2, 3, 4, 6, 12].
Usage
To use the provided function, call printDivisorsOptimal
with an integer argument:
vector<int> result = printDivisorsOptimal(12);
// result now contains the vector [1, 2, 3, 4, 6, 12], and it is printed as output