Print all combinations of factors (Ways to factorize)

Write a program to print all the combinations of factors of given number n.

Examples:

Input : 16
Output :2 2 2 2 
        2 2 4 
        2 8 
        4 4 

Input : 12
Output : 2 2 3
         2 6
         3 4



To solve this problem we take one array of array of integers or list of list of integers to store all the factors combination possible for the given n. So, to achieve this we can have one recursive function which can store the factors combination in each of its iteration. And each of those list should be stored in the final result list.

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print all factors combination
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
  
// defining vector of vector for 
// storing factor combinations
vector<vector<int>>resultant;
  
// current_factor is current factor to be considered. 
// current_product is current product of factors.     
void factorsListFunc (int first, int each_prod, int n,
                        vector<int>single_result_list) 
    // base case of this recursive function 
    if (first> n || each_prod > n) 
        return
  
    // When current_product is equal to n, 
    // then current contain list of factors 
    // so it will be added to the vect
    if (each_prod == n) 
    
        resultant.push_back(single_result_list); 
        return
    
  
    // In this loop we first calculate factors 
    // of n and then it's combination so that 
    // we get the value n in a recursive way . 
    for (int i = first; i < n; i++) 
    
        if (i * each_prod > n) 
            break
  
        // if i divides n 
        // properly then it 
        // is factor of n 
        if (n % i == 0) 
        
  
            // it is added to 'single_result_list' list 
            single_result_list.push_back(i); 
              
            // Here function is called recursively 
            // and when (i*each_prod) is equal to n 
            // we will store the 'single_result_list' 
            // (it is basically the list containing all 
            // combination of factors) into result_list. 
            factorsListFunc(i, i * each_prod, n, 
                            single_result_list); 
  
            // here we will empty the 'single_result_list' 
            // List so that new combination of factors 
            // get stored in it. 
            single_result_list.pop_back(); 
        
    
  
// Returns a list containing all ways 
// to factorize a number n. 
void factComb(int n) 
    // making list of lists to store all 
    // possible combinations of factors 
    vector<int>single_result_list;
      
    // function to calculate all the combinations 
    // of factors 
    factorsListFunc(2, 1, n, single_result_list); 
  
// Driver Code
int main()
{
    int n = 16; 
      
    // calling function for computing 
    // vector of vector 
    factComb(n); 
  
    // printing all possible combination 
    // of factors stored in vect 
    for (int i = 0; i < resultant.size(); i++)
    
        for (int j = 0; j < resultant[i].size(); j++) 
            cout << resultant[i][j] << " "
        cout << endl;
    }
    return 0;
}
  
// This code is contributed by 
// Atul kumar Shrivastava

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print all factors combination
import java.io.*;
import java.util.*;
class FactorsCombination {
  
    // Returns a list containing all ways to factorize
    // a number n.
    public static List<List<Integer> > factComb(int n)
    {
        // making list of lists to store all
        // possible combinations of factors
        List<List<Integer> > result_list =
                     new ArrayList<List<Integer> >();
        List<Integer> list = new ArrayList<Integer>();
  
        // function to calculate all the combinations
        // of factors
        factorsListFunc(2, 1, n, result_list, list);
        return result_list;
    }
  
    // First is current factor to be considered.
    // each_product is current product of factors.    
    public static void factorsListFunc(int first, 
                             int each_prod, int n, 
    List<List<Integer> > result_list, List<Integer>        
                               single_result_list)
    {
        // Terminating condition of this recursive 
        // function
        if (first > n || each_prod > n)
            return;
  
        // When each_prod is equal to n, we get
        // the list of factors in 'single_result_
        // _list' so it is  added to the result_
        // _list list .
        if (each_prod == n) {
  
            ArrayList<Integer> t =
          new ArrayList<Integer>(single_result_list);
  
            result_list.add(t);
  
            return;
        }
  
        // In this loop we first calculate factors
        // of n and then it's combination so that
        // we get the value n in a recursive way .
        for (int i = first; i < n; i++) {
            if (i * each_prod > n)
                break;
  
            // if i divides n
            // properly then it
            // is factor of n
            if (n % i == 0) {
  
                // it is added to 'single_result_list' list
                single_result_list.add(i);
  
                // Here function is called recursively
                // and when (i*each_prod) is equal to n
                // we will store the 'single_result_list' (it is
                // basically the list containing all
                // combination of factors) into result_list.
                factorsListFunc(i, i * each_prod, n,
                  result_list, single_result_list);
  
                // here we will empty the 'single_result_list' 
                // List so that new combination of factors
                // get stored in it.
                single_result_list.remove(single_result_list.size() - 1);
            }
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 16;
        List<List<Integer> > resultant = factComb(n);
  
        // printing all possible combination
        // of factors stored in resultant list
        for (List<Integer> i : resultant) {
            for (int j : i) 
                System.out.print(j + " ");            
            System.out.println();
        }
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print all factors combination
  
# current_factor is current factor to be considered. 
# current_product is current product of factors.     
def factorsListFunc (first, each_prod, n, single_result_list): 
      
    # base case of this recursive function 
    if (first> n or each_prod > n):
        return
  
    # When current_product is equal to n, 
    # then current contain list of factors
    # so it will be added to the vect
    if (each_prod == n) :
        print(*single_result_list)
        return
              
    # In this loop we first calculate factors 
    # of n and then it's combination so that 
    # we get the value n in a recursive way . 
    for i in range(first, n): 
        if (i * each_prod > n): 
            break
  
        # if i divides n properly, then it 
        # is factor of n 
        if (n % i == 0):
              
            # it is added to 'single_result_list' list 
            single_result_list.append(i)
              
            # Here function is called recursively 
            # and when (i*each_prod) is equal to n 
            # we will store the 'single_result_list' (it is 
            # basically the list containing all 
            # combination of factors) into result_list. 
            factorsListFunc(i, i * each_prod, n, 
                             single_result_list) 
              
            # here we will empty the 'single_result_list' 
            # List so that new combination of factors 
            # get stored in it. 
            single_result_list.remove(single_result_list[-1])
  
# Returns a list containing all ways 
# to factorize a number n.         
def factComb(n): 
      
    # making list of lists to store all 
    # possible combinations of factors 
    single_result_list = []
      
    # function to calculate all the combinations 
    # of factors 
    factorsListFunc(2, 1, n,single_result_list)
                      
# Driver Code
n = 16
  
# calling function for computing
# vector of vector 
factComb(n)
  
# This code is contributed by 
# Atul kumar Shrivastava 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print all factors combination
using System;
using System.Collections.Generic;
  
class GFG 
{
  
    // Returns a list containing all ways
    // to factorize a number n.
    public static List<List<int> > factComb(int n)
    {
        // making list of lists to store all
        // possible combinations of factors
        List<List<int> > result_list = 
                     new List<List<int> >();
        List<int> list = new List<int>();
  
        // function to calculate 
        // all the combinations of factors
        factorsListFunc(2, 1, n, result_list, list);
        return result_list;
    }
  
    // First is current factor to be considered.
    // each_product is current product of factors. 
    public static void factorsListFunc(int first, 
                            int each_prod, int n, 
                    List<List<int> > result_list,
                    List<int> single_result_list)
    {
        // Terminating condition of  
        // this recursive function
        if (first > n || each_prod > n)
            return;
  
        // When each_prod is equal to n, we get
        // the list of factors in 'single_result_
        // _list' so it is added to the result_
        // _list list .
        if (each_prod == n)
        {
            List<int> t = 
                new List<int>(single_result_list);
  
            result_list.Add(t);
  
            return;
        }
  
        // In this loop we first calculate factors
        // of n and then it's combination so that
        // we get the value n in a recursive way .
        for (int i = first; i < n; i++) 
        {
            if (i * each_prod > n)
                break;
  
            // if i divides n
            // properly then it
            // is factor of n
            if (n % i == 0) 
            {
  
                // it is added to 'single_result_list' list
                single_result_list.Add(i);
  
                // Here function is called recursively
                // and when (i*each_prod) is equal to n
                // we will store the 'single_result_list' (it is
                // basically the list containing all
                // combination of factors) into result_list.
                factorsListFunc(i, i * each_prod, n, 
                                        result_list, 
                                single_result_list);
  
                // here we will empty the 'single_result_list' 
                // List so that new combination of factors
                // get stored in it.
                single_result_list.RemoveAt(single_result_list.Count - 1);
            }
        }
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int n = 16;
        List<List<int> > resultant = factComb(n);
  
        // printing all possible combination
        // of factors stored in resultant list
        foreach (List<int> i in resultant) 
        {
            foreach (int j in i) 
                Console.Write(j + " ");         
            Console.WriteLine();
        }
    }
}
  
// This code is contributed by PrinciRaj1992 

chevron_right



Output:

2 2 2 2 
2 2 4 
2 8 
4 4


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.