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.
Java
// 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(); } } } |
C#
// 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 > factComb(int n)
{
// making list of lists to store all
// possible combinations of factors
List > result_list =
new List >();
List
// 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 > result_list,
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
new 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 > resultant = factComb(n);
// printing all possible combination
// of factors stored in resultant list
foreach (List
{
foreach (int j in i)
Console.Write(j + ” “);
Console.WriteLine();
}
}
}
// This code is contributed by PrinciRaj1992
Output:
2 2 2 2 2 2 4 2 8 4 4
Recommended Posts:
- Ways to express a number as product of two different factors
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Print all combinations of balanced parentheses
- Print combinations of distinct numbers which add up to give sum N
- Print all the combinations of a string in lexicographical order
- Print all possible combinations of r elements in a given array of size n
- Print all combinations of points that can compose a given number
- Print all prime factors and their powers
- Program to print factors of a number in pairs
- Efficient program to print all prime factors of a given number
- Efficient program to print the number of factors of n numbers
- C Program for efficiently print all prime factors of a given number
- Java Program for efficiently print all prime factors of a given number
- Find number of factors of N when location of its two factors whose product is N is given
- Combinations in a String of Digits
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.
Improved By : princiraj1992



