Given an integer N, the task is to find all of its divisors using its prime factorization.
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 it’s prime factorization as p1a1*p2a2*……*pkak, here 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 it’s occurrence.
Below is the implementation of the above approach:
1 3 2 6
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.