Generating all divisors of a number using its prime factorization
Given an integer N, the task is to find all of its divisors using its prime factorization.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input: N = 6
Output: 1 2 3 6
Input: N = 10
Output: 1 2 5 10
Approach: As every number greater than 1 can be represented in its prime factorization as p1a1*p2a2*……*pkak, where pi is a prime number, k ≥ 1 and ai is a positive integer.
Now all the possible divisors can be generated recursively if the count of occurrence of every prime factor of n is known. For every prime factor pi, it can be included x times where 0 ≤ x ≤ ai. First, find the prime factorization of n using this approach and for every prime factor, store it with the count of its occurrence.
Below is the implementation of the above approach:
1 3 2 6