Skip to main content

Distinct combinations of N length sequences whose product is M

Here given code implementation process.

// C Program
// Distinct combinations of N length sequences whose product is M
#include <stdio.h>

void subSequences(
    int result[], 
    int start, 
    int count, 
    int product, 
    int n, 
    int m)
{
    if (count == n && product == m)
    {
        for (int i = 0; i < count; ++i)
        {
            printf(" %d", result[i]);
        }
        printf("\n");
    }
    else if (product > m || count >= n)
    {
        return;
    }
    else
    {
        for (int i = start; i <= m; ++i)
        {
            // Collect elements 
            result[count] = i;

            subSequences(result, 
                i + 1, 
                count + 1, 
                product * i, 
                n, m);
        }
    }
}
// Handles the request to find resultant subsequences
void findSequence(int n, int m)
{
    if (n <= 0)
    {
        return;
    }
    printf("Given Length : %d \n",n);
    printf("Product      : %d \n",m);

    // Auxiliary array which is collect result
    int result[n];

    subSequences(result, 1, 0, 1, n, m);
}
int main()
{
    // Length of sequences
    int n = 3;
    // Product
    int m = 60;
    // Test
    findSequence(n, m);
    return 0;
}

Output

Given Length : 3
Product      : 60
 1 2 30
 1 3 20
 1 4 15
 1 5 12
 1 6 10
 2 3 10
 2 5 6
 3 4 5
// Java program for
// Distinct combinations of N length sequences whose product is M
public class Combination
{
   public void subSequences(int[] result, 
  		int start, 
  		int count, 
        int product, 
        int n, int m)
    {
        if (count == n && product == m)
        {
            for (int i = 0; i < count; ++i)
            {
                System.out.print(" " + result[i]);
            }
            System.out.print("\n");
        }
        else if (product > m || count >= n)
        {
            return;
        }
        else
        {
            for (int i = start; i <= m; ++i)
            {
                // Collect elements 
                result[count] = i;
                subSequences(result, i + 1, 
                             count + 1, product * i, n, m);
            }
        }
    }
    // Handles the request to find resultant subsequences
    public void findSequence(int n, int m)
    {
        if (n <= 0)
        {
            return;
        }
        System.out.print("Given Length : " + n + " \n");
        System.out.print("Product : " + m + " \n");
        // Auxiliary array which is collect result
        int[] result = new int[n];
        subSequences(result, 1, 0, 1, n, m);
    }
    public static void main(String[] args)
    {
        Combination task = new Combination();
        // Length of sequences
        int n = 3;
        // Product
        int m = 60;
        // Test
        task.findSequence(n, m);
    }
}

Output

Given Length : 3
Product : 60
 1 2 30
 1 3 20
 1 4 15
 1 5 12
 1 6 10
 2 3 10
 2 5 6
 3 4 5
// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Distinct combinations of N length sequences whose product is M
class Combination
{
	public: void subSequences(int result[], 
      int start, int count, 
        int product, int n, int m)
	{
		if (count == n && product == m)
		{
			for (int i = 0; i < count; ++i)
			{
				cout << " " << result[i];
			}
			cout << "\n";
		}
		else if (product > m || count >= n)
		{
			return;
		}
		else
		{
			for (int i = start; i <= m; ++i)
			{
				// Collect elements 
				result[count] = i;
				this->subSequences(result, i + 1, 
                                   count + 1, 
                                   product *i, n, m);
			}
		}
	}
	// Handles the request to find resultant subsequences
	void findSequence(int n, int m)
	{
		if (n <= 0)
		{
			return;
		}
		cout << "Given Length : " << n << " \n";
		cout << "Product : " << m << " \n";
		// Auxiliary array which is collect result
		int result[n];
		this->subSequences(result, 1, 0, 1, n, m);
	}
};
int main()
{
	Combination *task = new Combination();
	// Length of sequences
	int n = 3;
	// Product
	int m = 60;
	// Test
	task->findSequence(n, m);
	return 0;
}

Output

Given Length : 3
Product : 60
 1 2 30
 1 3 20
 1 4 15
 1 5 12
 1 6 10
 2 3 10
 2 5 6
 3 4 5
// Include namespace system
using System;
// Csharp program for
// Distinct combinations of N length sequences whose product is M
public class Combination
{
	public void subSequences(int[] result, 
  		int start, int count, 
  		int product, int n, int m)
	{
		if (count == n && product == m)
		{
			for (int i = 0; i < count; ++i)
			{
				Console.Write(" " + result[i]);
			}
			Console.Write("\n");
		}
		else if (product > m || count >= n)
		{
			return;
		}
		else
		{
			for (int i = start; i <= m; ++i)
			{
				// Collect elements 
				result[count] = i;
				this.subSequences(result, i + 1, 
                                  count + 1, product * i, 
                                  n, m);
			}
		}
	}
	// Handles the request to find resultant subsequences
	public void findSequence(int n, int m)
	{
		if (n <= 0)
		{
			return;
		}
		Console.Write("Given Length : " + n + " \n");
		Console.Write("Product : " + m + " \n");
		// Auxiliary array which is collect result
		int[] result = new int[n];
		this.subSequences(result, 1, 0, 1, n, m);
	}
	public static void Main(String[] args)
	{
		Combination task = new Combination();
		// Length of sequences
		int n = 3;
		// Product
		int m = 60;
		// Test
		task.findSequence(n, m);
	}
}

Output

Given Length : 3
Product : 60
 1 2 30
 1 3 20
 1 4 15
 1 5 12
 1 6 10
 2 3 10
 2 5 6
 3 4 5
package main
import "fmt"
// Go program for
// Distinct combinations of N length sequences whose product is M
type Combination struct {}
func getCombination() * Combination {
	var me *Combination = &Combination {}
	return me
}
func(this Combination) subSequences(result[] int, 
	start int, count int, 
	product int, 
	n int, 
	m int) {
	if count == n && product == m {
		for i := 0 ; i < count ; i++ {
			fmt.Print(" ", result[i])
		}
		fmt.Print("\n")
	} else if product > m || count >= n {
		return
	} else {
		for i := start ; i <= m ; i++ {
			// Collect elements 
			result[count] = i
			this.subSequences(result, i + 1, 
				count + 1, 
				product * i, 
				n, m)
		}
	}
}
// Handles the request to find resultant subsequences
func(this Combination) findSequence(n, m int) {
	if n <= 0 {
		return
	}
	fmt.Print("Given Length : ", n, " \n")
	fmt.Print("Product : ", m, " \n")
	// Auxiliary array which is collect result
	var result = make([] int, n)
	this.subSequences(result, 1, 0, 1, n, m)
}
func main() {
	var task * Combination = getCombination()
	// Length of sequences
	var n int = 3
	// Product
	var m int = 60
	// Test
	task.findSequence(n, m)
}

Output

Given Length : 3
Product : 60
 1 2 30
 1 3 20
 1 4 15
 1 5 12
 1 6 10
 2 3 10
 2 5 6
 3 4 5
<?php
// Php program for
// Distinct combinations of N length sequences whose product is M
class Combination
{
	public	function subSequences($result, 
                                   $start, $count, 
                                   $product, $n, $m)
	{
		if ($count == $n && $product == $m)
		{
			for ($i = 0; $i < $count; ++$i)
			{
				echo(" ".$result[$i]);
			}
			echo("\n");
		}
		else if ($product > $m || $count >= $n)
		{
			return;
		}
		else
		{
			for ($i = $start; $i <= $m; ++$i)
			{
				// Collect elements 
				$result[$count] = $i;
				$this->subSequences($result, $i + 1, 
                                    $count + 1, 
                                    $product * $i, $n, $m);
			}
		}
	}
	// Handles the request to find resultant subsequences
	public	function findSequence($n, $m)
	{
		if ($n <= 0)
		{
			return;
		}
		echo("Given Length : ".$n.
			" \n");
		echo("Product : ".$m.
			" \n");
		// Auxiliary array which is collect result
		$result = array_fill(0, $n, 0);
		$this->subSequences($result, 1, 0, 1, $n, $m);
	}
}

function main()
{
	$task = new Combination();
	// Length of sequences
	$n = 3;
	// Product
	$m = 60;
	// Test
	$task->findSequence($n, $m);
}
main();

Output

Given Length : 3
Product : 60
 1 2 30
 1 3 20
 1 4 15
 1 5 12
 1 6 10
 2 3 10
 2 5 6
 3 4 5
// Node JS program for
// Distinct combinations of N length sequences whose product is M
class Combination
{
	subSequences(result, start, count, product, n, m)
	{
		if (count == n && product == m)
		{
			for (var i = 0; i < count; ++i)
			{
				process.stdout.write(" " + result[i]);
			}
			process.stdout.write("\n");
		}
		else if (product > m || count >= n)
		{
			return;
		}
		else
		{
			for (var i = start; i <= m; ++i)
			{
				// Collect elements 
				result[count] = i;
				this.subSequences(result, i + 1, 
                                  count + 1, 
                                  product * i, 
                                  n, m);
			}
		}
	}
	// Handles the request to find resultant subsequences
	findSequence(n, m)
	{
		if (n <= 0)
		{
			return;
		}
		process.stdout.write("Given Length : " + n + " \n");
		process.stdout.write("Product : " + m + " \n");
		// Auxiliary array which is collect result
		var result = Array(n).fill(0);
		this.subSequences(result, 1, 0, 1, n, m);
	}
}

function main()
{
	var task = new Combination();
	// Length of sequences
	var n = 3;
	// Product
	var m = 60;
	// Test
	task.findSequence(n, m);
}
main();

Output

Given Length : 3
Product : 60
 1 2 30
 1 3 20
 1 4 15
 1 5 12
 1 6 10
 2 3 10
 2 5 6
 3 4 5
#  Python 3 program for
#  Distinct combinations of N length sequences whose product is M
class Combination :
	def subSequences(self, result, start, count, product, n, m) :
		if (count == n and product == m) :
			i = 0
			while (i < count) :
				print(" ", result[i], end = "")
				i += 1
			
			print(end = "\n")
		elif (product > m or count >= n) :
			return
		else :
			i = start
			while (i <= m) :
				#  Collect elements 
				result[count] = i
				self.subSequences(result, i + 1, 
                                  count + 1, 
                                  product * i, 
                                  n, m)
				i += 1
			
		
	
	#  Handles the request to find resultant subsequences
	def findSequence(self, n, m) :
		if (n <= 0) :
			return
		
		print("Given Length : ", n ," ")
		print("Product : ", m ," ")
		#  Auxiliary list which is collect result
		result = [0] * (n)
		self.subSequences(result, 1, 0, 1, n, m)
	

def main() :
	task = Combination()
	#  Length of sequences
	n = 3
	#  Product
	m = 60
	#  Test
	task.findSequence(n, m)

if __name__ == "__main__": main()

Output

Given Length :  3
Product :  60
  1  2  30
  1  3  20
  1  4  15
  1  5  12
  1  6  10
  2  3  10
  2  5  6
  3  4  5
#  Ruby program for
#  Distinct combinations of N length sequences whose product is M
class Combination 
	def subSequences(result, start, count, product, n, m) 
		if (count == n && product == m) 
			i = 0
			while (i < count) 
				print(" ", result[i])
				i += 1
			end

			print("\n")
		elsif (product > m || count >= n) 
			return
		else
 
			i = start
			while (i <= m) 
				#  Collect elements 
				result[count] = i
				self.subSequences(result, i + 1, 
                                  count + 1, 
                                  product * i, n, m)
				i += 1
			end

		end

	end

	#  Handles the request to find resultant subsequences
	def findSequence(n, m) 
		if (n <= 0) 
			return
		end

		print("Given Length : ", n ," \n")
		print("Product : ", m ," \n")
		#  Auxiliary array which is collect result
		result = Array.new(n) {0}
		self.subSequences(result, 1, 0, 1, n, m)
	end

end

def main() 
	task = Combination.new()
	#  Length of sequences
	n = 3
	#  Product
	m = 60
	#  Test
	task.findSequence(n, m)
end

main()

Output

Given Length : 3 
Product : 60 
 1 2 30
 1 3 20
 1 4 15
 1 5 12
 1 6 10
 2 3 10
 2 5 6
 3 4 5
// Scala program for
// Distinct combinations of N length sequences whose product is M
class Combination()
{
	def subSequences(result: Array[Int], 
      start: Int, count: Int, 
        product: Int, 
          n: Int, 
            m: Int): Unit = {
		if (count == n && product == m)
		{
			var i: Int = 0;
			while (i < count)
			{
				print(" " + result(i));
				i += 1;
			}
			print("\n");
		}
		else if (product > m || count >= n)
		{
			return;
		}
		else
		{
			var i: Int = start;
			while (i <= m)
			{
				// Collect elements 
				result(count) = i;
				subSequences(result, i + 1, 
                             count + 1, 
                             product * i, 
                             n, m);
				i += 1;
			}
		}
	}
	// Handles the request to find resultant subsequences
	def findSequence(n: Int, m: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		print("Given Length : " + n + " \n");
		print("Product : " + m + " \n");
		// Auxiliary array which is collect result
		var result: Array[Int] = Array.fill[Int](n)(0);
		subSequences(result, 1, 0, 1, n, m);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combination = new Combination();
		// Length of sequences
		var n: Int = 3;
		// Product
		var m: Int = 60;
		// Test
		task.findSequence(n, m);
	}
}

Output

Given Length : 3
Product : 60
 1 2 30
 1 3 20
 1 4 15
 1 5 12
 1 6 10
 2 3 10
 2 5 6
 3 4 5
// Swift 4 program for
// Distinct combinations of N length sequences whose product is M
class Combination
{
	func subSequences(_ result: inout[Int], 
 		 _ start: Int, 
    	 _ count: Int, 
         _ product: Int, 
           _ n: Int, 
             _ m: Int)
	{
		if (count == n && product == m)
		{
			var i: Int = 0;
			while (i < count)
			{
				print(" ", result[i], terminator: "");
				i += 1;
			}
			print(terminator: "\n");
		}
		else if (product > m || count >= n)
		{
			return;
		}
		else
		{
			var i: Int = start;
			while (i <= m)
			{
				// Collect elements 
				result[count] = i;
				self.subSequences(&result, i + 1, 
                                  count + 1, 
                                  product * i, 
                                  n, m);
				i += 1;
			}
		}
	}
	// Handles the request to find resultant subsequences
	func findSequence(_ n: Int, _ m: Int)
	{
		if (n <= 0)
		{
			return;
		}
		print("Given Length : ", n ," ");
		print("Product : ", m ," ");
		// Auxiliary array which is collect result
		var result: [Int] = Array(repeating: 0, count: n);
		self.subSequences(&result, 1, 0, 1, n, m);
	}
}
func main()
{
	let task: Combination = Combination();
	// Length of sequences
	let n: Int = 3;
	// Product
	let m: Int = 60;
	// Test
	task.findSequence(n, m);
}
main();

Output

Given Length :  3
Product :  60
  1  2  30
  1  3  20
  1  4  15
  1  5  12
  1  6  10
  2  3  10
  2  5  6
  3  4  5
// Kotlin program for
// Distinct combinations of N length sequences whose product is M
class Combination
{
	fun subSequences(result: Array < Int > , 
                      start: Int, count: Int, 
                      product: Int, 
                      n: Int, m: Int): Unit
	{
		if (count == n && product == m)
		{
			var i: Int = 0;
			while (i < count)
			{
				print(" " + result[i]);
				i += 1;
			}
			print("\n");
		}
		else if (product > m || count >= n)
		{
			return;
		}
		else
		{
			var i: Int = start;
			while (i <= m)
			{
				// Collect elements 
				result[count] = i;
				this.subSequences(result, i + 1, 
                                  count + 1, 
                                  product * i, n, m);
				i += 1;
			}
		}
	}
	// Handles the request to find resultant subsequences
	fun findSequence(n: Int, m: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		print("Given Length : " + n + " \n");
		print("Product : " + m + " \n");
		// Auxiliary array which is collect result
		var result: Array < Int > = Array(n)
		{
			0
		};
		this.subSequences(result, 1, 0, 1, n, m);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combination = Combination();
	// Length of sequences
	val n: Int = 3;
	// Product
	val m: Int = 60;
	// Test
	task.findSequence(n, m);
}

Output

Given Length : 3
Product : 60
 1 2 30
 1 3 20
 1 4 15
 1 5 12
 1 6 10
 2 3 10
 2 5 6
 3 4 5




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment