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.

Examples:

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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include "iostream"
#include "vector"
using namespace std;
  
struct primeFactorization {
  
    // to store the prime factor
    // and its highest power
    int countOfPf, primeFactor;
};
  
// Recursive function to generate all the
// divisors from the prime factors
void generateDivisors(int curIndex, int curDivisor,
                      vector<primeFactorization>& arr)
{
  
    // Base case i.e. we do not have more
    // primeFactors to include
    if (curIndex == arr.size()) {
        cout << curDivisor << ' ';
        return;
    }
  
    for (int i = 0; i <= arr[curIndex].countOfPf; ++i) {
        generateDivisors(curIndex + 1, curDivisor, arr);
        curDivisor *= arr[curIndex].primeFactor;
    }
}
  
// Function to find the divisors of n
void findDivisors(int n)
{
  
    // To store the prime factors along
    // with their highest power
    vector<primeFactorization> arr;
  
    // Finding prime factorization of n
    for (int i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            int count = 0;
            while (n % i == 0) {
                n /= i;
                count += 1;
            }
  
            // For every prime factor we are storing
            // count of it's occurenceand itself.
            arr.push_back({ count, i });
        }
    }
  
    // If n is prime
    if (n > 1) {
        arr.push_back({ 1, n });
    }
  
    int curIndex = 0, curDivisor = 1;
  
    // Generate all the divisors
    generateDivisors(curIndex, curDivisor, arr);
}
  
// Driver code
int main()
{
    int n = 6;
  
    findDivisors(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
class GFG 
{
  
static class primeFactorization 
{
  
    // to store the prime factor
    // and its highest power
    int countOfPf, primeFactor;
  
    public primeFactorization(int countOfPf, 
                              int primeFactor)
    {
        this.countOfPf = countOfPf;
        this.primeFactor = primeFactor;
    }
}
  
// Recursive function to generate all the
// divisors from the prime factors
static void generateDivisors(int curIndex, int curDivisor,
                           Vector<primeFactorization> arr)
{
  
    // Base case i.e. we do not have more
    // primeFactors to include
    if (curIndex == arr.size()) 
    {
        System.out.print(curDivisor + " ");
        return;
    }
  
    for (int i = 0; i <= arr.get(curIndex).countOfPf; ++i)
    {
        generateDivisors(curIndex + 1, curDivisor, arr);
        curDivisor *= arr.get(curIndex).primeFactor;
    }
}
  
// Function to find the divisors of n
static void findDivisors(int n)
{
  
    // To store the prime factors along
    // with their highest power
    Vector<primeFactorization> arr = new Vector<>();
  
    // Finding prime factorization of n
    for (int i = 2; i * i <= n; ++i)
    {
        if (n % i == 0)
        {
            int count = 0;
            while (n % i == 0
            {
                n /= i;
                count += 1;
            }
  
            // For every prime factor we are storing
            // count of it's occurenceand itself.
            arr.add(new primeFactorization(count, i ));
        }
    }
  
    // If n is prime
    if (n > 1)
    {
        arr.add(new primeFactorization( 1, n ));
    }
  
    int curIndex = 0, curDivisor = 1;
  
    // Generate all the divisors
    generateDivisors(curIndex, curDivisor, arr);
}
  
// Driver code
public static void main(String []args) 
{
    int n = 6;
  
    findDivisors(n);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Recursive function to generate all the 
# divisors from the prime factors 
def generateDivisors(curIndex, curDivisor, arr):
      
    # Base case i.e. we do not have more 
    # primeFactors to include 
    if (curIndex == len(arr)):
        print(curDivisor, end = ' ')
        return
      
    for i in range(arr[curIndex][0] + 1):
        generateDivisors(curIndex + 1, curDivisor, arr) 
        curDivisor *= arr[curIndex][1]
      
# Function to find the divisors of n 
def findDivisors(n):
      
    # To store the prime factors along 
    # with their highest power 
    arr = []
      
    # Finding prime factorization of n 
    i = 2
    while(i * i <= n):
        if (n % i == 0):
            count = 0
            while (n % i == 0):
                n //=
                count += 1
                  
            # For every prime factor we are storing 
            # count of it's occurenceand itself. 
            arr.append([count, i]) 
      
    # If n is prime 
    if (n > 1):
        arr.append([1, n]) 
      
    curIndex = 0
    curDivisor = 1
      
    # Generate all the divisors 
    generateDivisors(curIndex, curDivisor, arr) 
  
# Driver code 
n = 6
findDivisors(n) 
  
# This code is contributed by SHUBHAMSINGH10

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System; 
using System.Collections.Generic;
  
class GFG 
{
  
public class primeFactorization 
{
  
    // to store the prime factor
    // and its highest power
    public int countOfPf, primeFactor;
  
    public primeFactorization(int countOfPf, 
                              int primeFactor)
    {
        this.countOfPf = countOfPf;
        this.primeFactor = primeFactor;
    }
}
  
// Recursive function to generate all the
// divisors from the prime factors
static void generateDivisors(int curIndex, int curDivisor,
                             List<primeFactorization> arr)
{
  
    // Base case i.e. we do not have more
    // primeFactors to include
    if (curIndex == arr.Count) 
    {
        Console.Write(curDivisor + " ");
        return;
    }
  
    for (int i = 0; i <= arr[curIndex].countOfPf; ++i)
    {
        generateDivisors(curIndex + 1, curDivisor, arr);
        curDivisor *= arr[curIndex].primeFactor;
    }
}
  
// Function to find the divisors of n
static void findDivisors(int n)
{
  
    // To store the prime factors along
    // with their highest power
    List<primeFactorization> arr = new List<primeFactorization>();
  
    // Finding prime factorization of n
    for (int i = 2; i * i <= n; ++i)
    {
        if (n % i == 0)
        {
            int count = 0;
            while (n % i == 0) 
            {
                n /= i;
                count += 1;
            }
  
            // For every prime factor we are storing
            // count of it's occurenceand itself.
            arr.Add(new primeFactorization(count, i ));
        }
    }
  
    // If n is prime
    if (n > 1)
    {
        arr.Add(new primeFactorization( 1, n ));
    }
  
    int curIndex = 0, curDivisor = 1;
  
    // Generate all the divisors
    generateDivisors(curIndex, curDivisor, arr);
}
  
// Driver code
public static void Main(String []args) 
{
    int n = 6;
  
    findDivisors(n);
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output:

1 3 2 6


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.