Print all distinct combinations of sum x and n natural number

Here given code implementation process.

// C Program
// Print all distinct combinations of sum x and n natural number
#include <stdio.h>

// Display result
void printSequence(int result[], int k)
{
    for (int i = 0; i < k; ++i)
    {
        printf(" %d", result[i]);
    }
    printf("\n ");
}
void findCombination(int result[], int index, int sum, int x, int n)
{
    if (sum == x)
    {
        // Display calculated result
        printSequence(result, index);
        return;
    }
    if (sum > x || index >= x)
    {
        // Base case when stop process
        return;
    }
    for (int i = n; i >= 1; --i)
    {
        // Collect result value
        result[index] = i;
        findCombination(result, index + 1, sum + i, x, i);
    }
}
// Handles the request of find combination of given n and x
void combination(int x,int n)
{
    if (n <= 0 || x <= 0)
    {
        return;
    }
    printf("\n Given n : %d  x : %d \n ", n,x);
    // Collect result
    int result[x];
    // Test
    findCombination(result, 0, 0, x, n);
}
int main()
{   
  	// Test A
    // n = 3 means use [1,2,3]
    int n = 3;
    // Sum x = 10
    int x = 10;
    combination(x,n);
  	
  	// Test B
  	// n = 4 means use [1,2,3,4]
    n = 4;
    // Sum x = 12
    x = 12;
    combination(x,n);
  
  
    return 0;
}

Output

 Given n : 3  x : 10
  3 3 3 1
  3 3 2 2
  3 3 2 1 1
  3 3 1 1 1 1
  3 2 2 2 1
  3 2 2 1 1 1
  3 2 1 1 1 1 1
  3 1 1 1 1 1 1 1
  2 2 2 2 2
  2 2 2 2 1 1
  2 2 2 1 1 1 1
  2 2 1 1 1 1 1 1
  2 1 1 1 1 1 1 1 1
  1 1 1 1 1 1 1 1 1 1

 Given n : 4  x : 12
  4 4 4
  4 4 3 1
  4 4 2 2
  4 4 2 1 1
  4 4 1 1 1 1
  4 3 3 2
  4 3 3 1 1
  4 3 2 2 1
  4 3 2 1 1 1
  4 3 1 1 1 1 1
  4 2 2 2 2
  4 2 2 2 1 1
  4 2 2 1 1 1 1
  4 2 1 1 1 1 1 1
  4 1 1 1 1 1 1 1 1
  3 3 3 3
  3 3 3 2 1
  3 3 3 1 1 1
  3 3 2 2 2
  3 3 2 2 1 1
  3 3 2 1 1 1 1
  3 3 1 1 1 1 1 1
  3 2 2 2 2 1
  3 2 2 2 1 1 1
  3 2 2 1 1 1 1 1
  3 2 1 1 1 1 1 1 1
  3 1 1 1 1 1 1 1 1 1
  2 2 2 2 2 2
  2 2 2 2 2 1 1
  2 2 2 2 1 1 1 1
  2 2 2 1 1 1 1 1 1
  2 2 1 1 1 1 1 1 1 1
  2 1 1 1 1 1 1 1 1 1 1
  1 1 1 1 1 1 1 1 1 1 1 1
// Java Program for
// Print all distinct combinations of sum x and n natural number
public class SumCombination
{
	// Display result
	public void printSequence(int[] result, int k)
	{
		for (int i = 0; i < k; ++i)
		{
			System.out.print("  " + result[i]);
		}
		System.out.print("\n");
	}
	public void findCombination(int[] result, 
                                int index, 
                                int sum, 
                                int x, 
                                int n)
	{
		if (sum == x)
		{
			// Display calculated result
			printSequence(result, index);
			return;
		}
		if (sum > x || index >= x)
		{
			// Base case when stop process
			return;
		}
		for (int i = n; i >= 1; --i)
		{
			// Collect result value
			result[index] = i;
			findCombination(result, index + 1, sum + i, x, i);
		}
	}
	// Handles the request of find combination of given n and x
	public void combination(int x, int n)
	{
		if (n <= 0 || x <= 0)
		{
			return;
		}
		System.out.println("\n Given n : " + n + " x : " + x);
		// Collect result
		int[] result = new int[x];
		// Test
		findCombination(result, 0, 0, x, n);
	}
	public static void main(String[] args)
	{
		SumCombination task = new SumCombination();
		// Test A
		// n = 3 means use [1,2,3]
		int n = 3;
		// Sum x = 10
		int x = 10;
		task.combination(x, n);
		// Test B
		// n = 4 means use [1,2,3,4]
		n = 4;
		// Sum x = 12
		x = 12;
		task.combination(x, n);
	}
}

Output

 Given n : 3 x : 10
  3  3  3  1
  3  3  2  2
  3  3  2  1  1
  3  3  1  1  1  1
  3  2  2  2  1
  3  2  2  1  1  1
  3  2  1  1  1  1  1
  3  1  1  1  1  1  1  1
  2  2  2  2  2
  2  2  2  2  1  1
  2  2  2  1  1  1  1
  2  2  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1

 Given n : 4 x : 12
  4  4  4
  4  4  3  1
  4  4  2  2
  4  4  2  1  1
  4  4  1  1  1  1
  4  3  3  2
  4  3  3  1  1
  4  3  2  2  1
  4  3  2  1  1  1
  4  3  1  1  1  1  1
  4  2  2  2  2
  4  2  2  2  1  1
  4  2  2  1  1  1  1
  4  2  1  1  1  1  1  1
  4  1  1  1  1  1  1  1  1
  3  3  3  3
  3  3  3  2  1
  3  3  3  1  1  1
  3  3  2  2  2
  3  3  2  2  1  1
  3  3  2  1  1  1  1
  3  3  1  1  1  1  1  1
  3  2  2  2  2  1
  3  2  2  2  1  1  1
  3  2  2  1  1  1  1  1
  3  2  1  1  1  1  1  1  1
  3  1  1  1  1  1  1  1  1  1
  2  2  2  2  2  2
  2  2  2  2  2  1  1
  2  2  2  2  1  1  1  1
  2  2  2  1  1  1  1  1  1
  2  2  1  1  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1
// Include header file
#include <iostream>

using namespace std;
// C++ Program for
// Print all distinct combinations of sum x and n natural number
class SumCombination
{
	public:
		// Display result
		void printSequence(int result[], int k)
		{
			for (int i = 0; i < k; ++i)
			{
				cout << "  " << result[i];
			}
			cout << "\n";
		}
	void findCombination(int result[], int index, int sum, int x, int n)
	{
		if (sum == x)
		{
			// Display calculated result
			this->printSequence(result, index);
			return;
		}
		if (sum > x || index >= x)
		{
			// Base case when stop process
			return;
		}
		for (int i = n; i >= 1; --i)
		{
			// Collect result value
			result[index] = i;
			this->findCombination(result, index + 1, sum + i, x, i);
		}
	}
	// Handles the request of find combination of given n and x
	void combination(int x, int n)
	{
		if (n <= 0 || x <= 0)
		{
			return;
		}
		cout << "\n Given n : " << n << " x : " << x << endl;
		// Collect result
		int result[x];
		// Test
		this->findCombination(result, 0, 0, x, n);
	}
};
int main()
{
	SumCombination *task = new SumCombination();
	// Test A
	// n = 3 means use [1,2,3]
	int n = 3;
	// Sum x = 10
	int x = 10;
	task->combination(x, n);
	// Test B
	// n = 4 means use [1,2,3,4]
	n = 4;
	// Sum x = 12
	x = 12;
	task->combination(x, n);
	return 0;
}

Output

 Given n : 3 x : 10
  3  3  3  1
  3  3  2  2
  3  3  2  1  1
  3  3  1  1  1  1
  3  2  2  2  1
  3  2  2  1  1  1
  3  2  1  1  1  1  1
  3  1  1  1  1  1  1  1
  2  2  2  2  2
  2  2  2  2  1  1
  2  2  2  1  1  1  1
  2  2  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1

 Given n : 4 x : 12
  4  4  4
  4  4  3  1
  4  4  2  2
  4  4  2  1  1
  4  4  1  1  1  1
  4  3  3  2
  4  3  3  1  1
  4  3  2  2  1
  4  3  2  1  1  1
  4  3  1  1  1  1  1
  4  2  2  2  2
  4  2  2  2  1  1
  4  2  2  1  1  1  1
  4  2  1  1  1  1  1  1
  4  1  1  1  1  1  1  1  1
  3  3  3  3
  3  3  3  2  1
  3  3  3  1  1  1
  3  3  2  2  2
  3  3  2  2  1  1
  3  3  2  1  1  1  1
  3  3  1  1  1  1  1  1
  3  2  2  2  2  1
  3  2  2  2  1  1  1
  3  2  2  1  1  1  1  1
  3  2  1  1  1  1  1  1  1
  3  1  1  1  1  1  1  1  1  1
  2  2  2  2  2  2
  2  2  2  2  2  1  1
  2  2  2  2  1  1  1  1
  2  2  2  1  1  1  1  1  1
  2  2  1  1  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1
package main
import "fmt"
// Go Program for
// Print all distinct combinations of sum x and n natural number
type SumCombination struct {}
func getSumCombination() * SumCombination {
	var me *SumCombination = &SumCombination {}
	return me
}
// Display result
func(this SumCombination) printSequence(result[] int, k int) {
	for i := 0 ; i < k ; i++ {
		fmt.Print("  ", result[i])
	}
	fmt.Print("\n")
}
func(this SumCombination) findCombination(result[] int, 
                           index int, sum int, x int, n int) {
	if sum == x {
		// Display calculated result
		this.printSequence(result, index)
		return
	}
	if sum > x || index >= x {
		// Base case when stop process
		return
	}
	for i := n ; i >= 1 ; i-- {
		// Collect result value
		result[index] = i
		this.findCombination(result, index + 1, sum + i, x, i)
	}
}
// Handles the request of find combination of given n and x
func(this SumCombination) combination(x, n int) {
	if n <= 0 || x <= 0 {
		return
	}
	fmt.Println("\n Given n : ", n, " x : ", x)
	// Collect result
	var result = make([] int, x)
	// Test
	this.findCombination(result, 0, 0, x, n)
}
func main() {
	var task * SumCombination = getSumCombination()
	// Test A
	// n = 3 means use [1,2,3]
	var n int = 3
	// Sum x = 10
	var x int = 10
	task.combination(x, n)
	// Test B
	// n = 4 means use [1,2,3,4]
	n = 4
	// Sum x = 12
	x = 12
	task.combination(x, n)
}

Output

 Given n : 3 x : 10
  3  3  3  1
  3  3  2  2
  3  3  2  1  1
  3  3  1  1  1  1
  3  2  2  2  1
  3  2  2  1  1  1
  3  2  1  1  1  1  1
  3  1  1  1  1  1  1  1
  2  2  2  2  2
  2  2  2  2  1  1
  2  2  2  1  1  1  1
  2  2  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1

 Given n : 4 x : 12
  4  4  4
  4  4  3  1
  4  4  2  2
  4  4  2  1  1
  4  4  1  1  1  1
  4  3  3  2
  4  3  3  1  1
  4  3  2  2  1
  4  3  2  1  1  1
  4  3  1  1  1  1  1
  4  2  2  2  2
  4  2  2  2  1  1
  4  2  2  1  1  1  1
  4  2  1  1  1  1  1  1
  4  1  1  1  1  1  1  1  1
  3  3  3  3
  3  3  3  2  1
  3  3  3  1  1  1
  3  3  2  2  2
  3  3  2  2  1  1
  3  3  2  1  1  1  1
  3  3  1  1  1  1  1  1
  3  2  2  2  2  1
  3  2  2  2  1  1  1
  3  2  2  1  1  1  1  1
  3  2  1  1  1  1  1  1  1
  3  1  1  1  1  1  1  1  1  1
  2  2  2  2  2  2
  2  2  2  2  2  1  1
  2  2  2  2  1  1  1  1
  2  2  2  1  1  1  1  1  1
  2  2  1  1  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1
// Include namespace system
using System;
// Csharp Program for
// Print all distinct combinations of sum x and n natural number
public class SumCombination
{
	// Display result
	public void printSequence(int[] result, int k)
	{
		for (int i = 0; i < k; ++i)
		{
			Console.Write("  " + result[i]);
		}
		Console.Write("\n");
	}
	public void findCombination(int[] result, 
                                int index, 
                                int sum, 
                                int x, 
                                int n)
	{
		if (sum == x)
		{
			// Display calculated result
			this.printSequence(result, index);
			return;
		}
		if (sum > x || index >= x)
		{
			// Base case when stop process
			return;
		}
		for (int i = n; i >= 1; --i)
		{
			// Collect result value
			result[index] = i;
			this.findCombination(result, index + 1, sum + i, x, i);
		}
	}
	// Handles the request of find combination of given n and x
	public void combination(int x, int n)
	{
		if (n <= 0 || x <= 0)
		{
			return;
		}
		Console.WriteLine("\n Given n : " + n + " x : " + x);
		// Collect result
		int[] result = new int[x];
		// Test
		this.findCombination(result, 0, 0, x, n);
	}
	public static void Main(String[] args)
	{
		SumCombination task = new SumCombination();
		// Test A
		// n = 3 means use [1,2,3]
		int n = 3;
		// Sum x = 10
		int x = 10;
		task.combination(x, n);
		// Test B
		// n = 4 means use [1,2,3,4]
		n = 4;
		// Sum x = 12
		x = 12;
		task.combination(x, n);
	}
}

Output

 Given n : 3 x : 10
  3  3  3  1
  3  3  2  2
  3  3  2  1  1
  3  3  1  1  1  1
  3  2  2  2  1
  3  2  2  1  1  1
  3  2  1  1  1  1  1
  3  1  1  1  1  1  1  1
  2  2  2  2  2
  2  2  2  2  1  1
  2  2  2  1  1  1  1
  2  2  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1

 Given n : 4 x : 12
  4  4  4
  4  4  3  1
  4  4  2  2
  4  4  2  1  1
  4  4  1  1  1  1
  4  3  3  2
  4  3  3  1  1
  4  3  2  2  1
  4  3  2  1  1  1
  4  3  1  1  1  1  1
  4  2  2  2  2
  4  2  2  2  1  1
  4  2  2  1  1  1  1
  4  2  1  1  1  1  1  1
  4  1  1  1  1  1  1  1  1
  3  3  3  3
  3  3  3  2  1
  3  3  3  1  1  1
  3  3  2  2  2
  3  3  2  2  1  1
  3  3  2  1  1  1  1
  3  3  1  1  1  1  1  1
  3  2  2  2  2  1
  3  2  2  2  1  1  1
  3  2  2  1  1  1  1  1
  3  2  1  1  1  1  1  1  1
  3  1  1  1  1  1  1  1  1  1
  2  2  2  2  2  2
  2  2  2  2  2  1  1
  2  2  2  2  1  1  1  1
  2  2  2  1  1  1  1  1  1
  2  2  1  1  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1
<?php
// Php Program for
// Print all distinct combinations of sum x and n natural number
class SumCombination
{
	// Display result
	public	function printSequence($result, $k)
	{
		for ($i = 0; $i < $k; ++$i)
		{
			echo("  ".$result[$i]);
		}
		echo("\n");
	}
	public	function findCombination($result, $index, $sum, $x, $n)
	{
		if ($sum == $x)
		{
			// Display calculated result
			$this->printSequence($result, $index);
			return;
		}
		if ($sum > $x || $index >= $x)
		{
			// Base case when stop process
			return;
		}
		for ($i = $n; $i >= 1; --$i)
		{
			// Collect result value
			$result[$index] = $i;
			$this->findCombination($result, $index + 1, $sum + $i, $x, $i);
		}
	}
	// Handles the request of find combination of given n and x
	public	function combination($x, $n)
	{
		if ($n <= 0 || $x <= 0)
		{
			return;
		}
		echo("\n Given n : ".$n.
			" x : ".$x.
			"\n");
		// Collect result
		$result = array_fill(0, $x, 0);
		// Test
		$this->findCombination($result, 0, 0, $x, $n);
	}
}

function main()
{
	$task = new SumCombination();
	// Test A
	// n = 3 means use [1,2,3]
	$n = 3;
	// Sum x = 10
	$x = 10;
	$task->combination($x, $n);
	// Test B
	// n = 4 means use [1,2,3,4]
	$n = 4;
	// Sum x = 12
	$x = 12;
	$task->combination($x, $n);
}
main();

Output

 Given n : 3 x : 10
  3  3  3  1
  3  3  2  2
  3  3  2  1  1
  3  3  1  1  1  1
  3  2  2  2  1
  3  2  2  1  1  1
  3  2  1  1  1  1  1
  3  1  1  1  1  1  1  1
  2  2  2  2  2
  2  2  2  2  1  1
  2  2  2  1  1  1  1
  2  2  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1

 Given n : 4 x : 12
  4  4  4
  4  4  3  1
  4  4  2  2
  4  4  2  1  1
  4  4  1  1  1  1
  4  3  3  2
  4  3  3  1  1
  4  3  2  2  1
  4  3  2  1  1  1
  4  3  1  1  1  1  1
  4  2  2  2  2
  4  2  2  2  1  1
  4  2  2  1  1  1  1
  4  2  1  1  1  1  1  1
  4  1  1  1  1  1  1  1  1
  3  3  3  3
  3  3  3  2  1
  3  3  3  1  1  1
  3  3  2  2  2
  3  3  2  2  1  1
  3  3  2  1  1  1  1
  3  3  1  1  1  1  1  1
  3  2  2  2  2  1
  3  2  2  2  1  1  1
  3  2  2  1  1  1  1  1
  3  2  1  1  1  1  1  1  1
  3  1  1  1  1  1  1  1  1  1
  2  2  2  2  2  2
  2  2  2  2  2  1  1
  2  2  2  2  1  1  1  1
  2  2  2  1  1  1  1  1  1
  2  2  1  1  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1
// Node JS Program for
// Print all distinct combinations of sum x and n natural number
class SumCombination
{
	// Display result
	printSequence(result, k)
	{
		for (var i = 0; i < k; ++i)
		{
			process.stdout.write("  " + result[i]);
		}
		process.stdout.write("\n");
	}
	findCombination(result, index, sum, x, n)
	{
		if (sum == x)
		{
			// Display calculated result
			this.printSequence(result, index);
			return;
		}
		if (sum > x || index >= x)
		{
			// Base case when stop process
			return;
		}
		for (var i = n; i >= 1; --i)
		{
			// Collect result value
			result[index] = i;
			this.findCombination(result, index + 1, sum + i, x, i);
		}
	}
	// Handles the request of find combination of given n and x
	combination(x, n)
	{
		if (n <= 0 || x <= 0)
		{
			return;
		}
		console.log("\n Given n : " + n + " x : " + x);
		// Collect result
		var result = Array(x).fill(0);
		// Test
		this.findCombination(result, 0, 0, x, n);
	}
}

function main()
{
	var task = new SumCombination();
	// Test A
	// n = 3 means use [1,2,3]
	var n = 3;
	// Sum x = 10
	var x = 10;
	task.combination(x, n);
	// Test B
	// n = 4 means use [1,2,3,4]
	n = 4;
	// Sum x = 12
	x = 12;
	task.combination(x, n);
}
main();

Output

 Given n : 3 x : 10
  3  3  3  1
  3  3  2  2
  3  3  2  1  1
  3  3  1  1  1  1
  3  2  2  2  1
  3  2  2  1  1  1
  3  2  1  1  1  1  1
  3  1  1  1  1  1  1  1
  2  2  2  2  2
  2  2  2  2  1  1
  2  2  2  1  1  1  1
  2  2  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1

 Given n : 4 x : 12
  4  4  4
  4  4  3  1
  4  4  2  2
  4  4  2  1  1
  4  4  1  1  1  1
  4  3  3  2
  4  3  3  1  1
  4  3  2  2  1
  4  3  2  1  1  1
  4  3  1  1  1  1  1
  4  2  2  2  2
  4  2  2  2  1  1
  4  2  2  1  1  1  1
  4  2  1  1  1  1  1  1
  4  1  1  1  1  1  1  1  1
  3  3  3  3
  3  3  3  2  1
  3  3  3  1  1  1
  3  3  2  2  2
  3  3  2  2  1  1
  3  3  2  1  1  1  1
  3  3  1  1  1  1  1  1
  3  2  2  2  2  1
  3  2  2  2  1  1  1
  3  2  2  1  1  1  1  1
  3  2  1  1  1  1  1  1  1
  3  1  1  1  1  1  1  1  1  1
  2  2  2  2  2  2
  2  2  2  2  2  1  1
  2  2  2  2  1  1  1  1
  2  2  2  1  1  1  1  1  1
  2  2  1  1  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1
#  Python 3 Program for
#  Print all distinct combinations of sum x and n natural number
class SumCombination :
	#  Display result
	def printSequence(self, result, k) :
		i = 0
		while (i < k) :
			print(" ", result[i], end = "")
			i += 1
		
		print(end = "\n")
	
	def findCombination(self, result, index, sum, x, n) :
		if (sum == x) :
			#  Display calculated result
			self.printSequence(result, index)
			return
		
		if (sum > x or index >= x) :
			#  Base case when stop process
			return
		
		i = n
		while (i >= 1) :
			#  Collect result value
			result[index] = i
			self.findCombination(result, index + 1, sum + i, x, i)
			i -= 1
		
	
	#  Handles the request of find combination of given n and x
	def combination(self, x, n) :
		if (n <= 0 or x <= 0) :
			return
		
		print("\n Given n : ", n ," x : ", x)
		#  Collect result
		result = [0] * (x)
		#  Test
		self.findCombination(result, 0, 0, x, n)
	

def main() :
	task = SumCombination()
	#  Test A
	#  n = 3 means use [1,2,3]
	n = 3
	#  Sum x = 10
	x = 10
	task.combination(x, n)
	#  Test B
	#  n = 4 means use [1,2,3,4]
	n = 4
	#  Sum x = 12
	x = 12
	task.combination(x, n)

if __name__ == "__main__": main()

Output

 Given n :  3  x :  10
  3  3  3  1
  3  3  2  2
  3  3  2  1  1
  3  3  1  1  1  1
  3  2  2  2  1
  3  2  2  1  1  1
  3  2  1  1  1  1  1
  3  1  1  1  1  1  1  1
  2  2  2  2  2
  2  2  2  2  1  1
  2  2  2  1  1  1  1
  2  2  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1

 Given n :  4  x :  12
  4  4  4
  4  4  3  1
  4  4  2  2
  4  4  2  1  1
  4  4  1  1  1  1
  4  3  3  2
  4  3  3  1  1
  4  3  2  2  1
  4  3  2  1  1  1
  4  3  1  1  1  1  1
  4  2  2  2  2
  4  2  2  2  1  1
  4  2  2  1  1  1  1
  4  2  1  1  1  1  1  1
  4  1  1  1  1  1  1  1  1
  3  3  3  3
  3  3  3  2  1
  3  3  3  1  1  1
  3  3  2  2  2
  3  3  2  2  1  1
  3  3  2  1  1  1  1
  3  3  1  1  1  1  1  1
  3  2  2  2  2  1
  3  2  2  2  1  1  1
  3  2  2  1  1  1  1  1
  3  2  1  1  1  1  1  1  1
  3  1  1  1  1  1  1  1  1  1
  2  2  2  2  2  2
  2  2  2  2  2  1  1
  2  2  2  2  1  1  1  1
  2  2  2  1  1  1  1  1  1
  2  2  1  1  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1
#  Ruby Program for
#  Print all distinct combinations of sum x and n natural number
class SumCombination 
	#  Display result
	def printSequence(result, k) 
		i = 0
		while (i < k) 
			print("  ", result[i])
			i += 1
		end

		print("\n")
	end

	def findCombination(result, index, sum, x, n) 
		if (sum == x) 
			#  Display calculated result
			self.printSequence(result, index)
			return
		end

		if (sum > x || index >= x) 
			#  Base case when stop process
			return
		end

		i = n
		while (i >= 1) 
			#  Collect result value
			result[index] = i
			self.findCombination(result, index + 1, sum + i, x, i)
			i -= 1
		end

	end

	#  Handles the request of find combination of given n and x
	def combination(x, n) 
		if (n <= 0 || x <= 0) 
			return
		end

		print("\n Given n : ", n ," x : ", x, "\n")
		#  Collect result
		result = Array.new(x) {0}
		#  Test
		self.findCombination(result, 0, 0, x, n)
	end

end

def main() 
	task = SumCombination.new()
	#  Test A
	#  n = 3 means use [1,2,3]
	n = 3
	#  Sum x = 10
	x = 10
	task.combination(x, n)
	#  Test B
	#  n = 4 means use [1,2,3,4]
	n = 4
	#  Sum x = 12
	x = 12
	task.combination(x, n)
end

main()

Output

 Given n : 3 x : 10
  3  3  3  1
  3  3  2  2
  3  3  2  1  1
  3  3  1  1  1  1
  3  2  2  2  1
  3  2  2  1  1  1
  3  2  1  1  1  1  1
  3  1  1  1  1  1  1  1
  2  2  2  2  2
  2  2  2  2  1  1
  2  2  2  1  1  1  1
  2  2  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1

 Given n : 4 x : 12
  4  4  4
  4  4  3  1
  4  4  2  2
  4  4  2  1  1
  4  4  1  1  1  1
  4  3  3  2
  4  3  3  1  1
  4  3  2  2  1
  4  3  2  1  1  1
  4  3  1  1  1  1  1
  4  2  2  2  2
  4  2  2  2  1  1
  4  2  2  1  1  1  1
  4  2  1  1  1  1  1  1
  4  1  1  1  1  1  1  1  1
  3  3  3  3
  3  3  3  2  1
  3  3  3  1  1  1
  3  3  2  2  2
  3  3  2  2  1  1
  3  3  2  1  1  1  1
  3  3  1  1  1  1  1  1
  3  2  2  2  2  1
  3  2  2  2  1  1  1
  3  2  2  1  1  1  1  1
  3  2  1  1  1  1  1  1  1
  3  1  1  1  1  1  1  1  1  1
  2  2  2  2  2  2
  2  2  2  2  2  1  1
  2  2  2  2  1  1  1  1
  2  2  2  1  1  1  1  1  1
  2  2  1  1  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1
// Scala Program for
// Print all distinct combinations of sum x and n natural number
class SumCombination()
{
	// Display result
	def printSequence(result: Array[Int], k: Int): Unit = {
		var i: Int = 0;
		while (i < k)
		{
			print("  " + result(i));
			i += 1;
		}
		print("\n");
	}
	def findCombination(result: Array[Int], 
      index: Int, sum: Int, x: Int, n: Int): Unit = {
		if (sum == x)
		{
			// Display calculated result
			printSequence(result, index);
			return;
		}
		if (sum > x || index >= x)
		{
			// Base case when stop process
			return;
		}
		var i: Int = n;
		while (i >= 1)
		{
			// Collect result value
			result(index) = i;
			findCombination(result, index + 1, sum + i, x, i);
			i -= 1;
		}
	}
	// Handles the request of find combination of given n and x
	def combination(x: Int, n: Int): Unit = {
		if (n <= 0 || x <= 0)
		{
			return;
		}
		println("\n Given n : " + n + " x : " + x);
		// Collect result
		var result: Array[Int] = Array.fill[Int](x)(0);
		// Test
		findCombination(result, 0, 0, x, n);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: SumCombination = new SumCombination();
		// Test A
		// n = 3 means use [1,2,3]
		var n: Int = 3;
		// Sum x = 10
		var x: Int = 10;
		task.combination(x, n);
		// Test B
		// n = 4 means use [1,2,3,4]
		n = 4;
		// Sum x = 12
		x = 12;
		task.combination(x, n);
	}
}

Output

 Given n : 3 x : 10
  3  3  3  1
  3  3  2  2
  3  3  2  1  1
  3  3  1  1  1  1
  3  2  2  2  1
  3  2  2  1  1  1
  3  2  1  1  1  1  1
  3  1  1  1  1  1  1  1
  2  2  2  2  2
  2  2  2  2  1  1
  2  2  2  1  1  1  1
  2  2  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1

 Given n : 4 x : 12
  4  4  4
  4  4  3  1
  4  4  2  2
  4  4  2  1  1
  4  4  1  1  1  1
  4  3  3  2
  4  3  3  1  1
  4  3  2  2  1
  4  3  2  1  1  1
  4  3  1  1  1  1  1
  4  2  2  2  2
  4  2  2  2  1  1
  4  2  2  1  1  1  1
  4  2  1  1  1  1  1  1
  4  1  1  1  1  1  1  1  1
  3  3  3  3
  3  3  3  2  1
  3  3  3  1  1  1
  3  3  2  2  2
  3  3  2  2  1  1
  3  3  2  1  1  1  1
  3  3  1  1  1  1  1  1
  3  2  2  2  2  1
  3  2  2  2  1  1  1
  3  2  2  1  1  1  1  1
  3  2  1  1  1  1  1  1  1
  3  1  1  1  1  1  1  1  1  1
  2  2  2  2  2  2
  2  2  2  2  2  1  1
  2  2  2  2  1  1  1  1
  2  2  2  1  1  1  1  1  1
  2  2  1  1  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1
// Swift 4 Program for
// Print all distinct combinations of sum x and n natural number
class SumCombination
{
	// Display result
	func printSequence(_ result: [Int], _ k: Int)
	{
		var i: Int = 0;
		while (i < k)
		{
			print(" ", result[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	func findCombination(_ result: inout[Int], 
      _ index: Int, _ sum: Int, _ x: Int, _ n: Int)
	{
		if (sum == x)
		{
			// Display calculated result
			self.printSequence(result, index);
			return;
		}
		if (sum > x || index >= x)
		{
			// Base case when stop process
			return;
		}
		var i: Int = n;
		while (i >= 1)
		{
			// Collect result value
			result[index] = i;
			self.findCombination(&result, index + 1, sum + i, x, i);
			i -= 1;
		}
	}
	// Handles the request of find combination of given n and x
	func combination(_ x: Int, _ n: Int)
	{
		if (n <= 0 || x <= 0)
		{
			return;
		}
		print("\n Given n : ", n ," x : ", x);
		// Collect result
		var result: [Int] = Array(repeating: 0, count: x);
		// Test
		self.findCombination(&result, 0, 0, x, n);
	}
}
func main()
{
	let task: SumCombination = SumCombination();
	// Test A
	// n = 3 means use [1,2,3]
	var n: Int = 3;
	// Sum x = 10
	var x: Int = 10;
	task.combination(x, n);
	// Test B
	// n = 4 means use [1,2,3,4]
	n = 4;
	// Sum x = 12
	x = 12;
	task.combination(x, n);
}
main();

Output

 Given n :  3  x :  10
  3  3  3  1
  3  3  2  2
  3  3  2  1  1
  3  3  1  1  1  1
  3  2  2  2  1
  3  2  2  1  1  1
  3  2  1  1  1  1  1
  3  1  1  1  1  1  1  1
  2  2  2  2  2
  2  2  2  2  1  1
  2  2  2  1  1  1  1
  2  2  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1

 Given n :  4  x :  12
  4  4  4
  4  4  3  1
  4  4  2  2
  4  4  2  1  1
  4  4  1  1  1  1
  4  3  3  2
  4  3  3  1  1
  4  3  2  2  1
  4  3  2  1  1  1
  4  3  1  1  1  1  1
  4  2  2  2  2
  4  2  2  2  1  1
  4  2  2  1  1  1  1
  4  2  1  1  1  1  1  1
  4  1  1  1  1  1  1  1  1
  3  3  3  3
  3  3  3  2  1
  3  3  3  1  1  1
  3  3  2  2  2
  3  3  2  2  1  1
  3  3  2  1  1  1  1
  3  3  1  1  1  1  1  1
  3  2  2  2  2  1
  3  2  2  2  1  1  1
  3  2  2  1  1  1  1  1
  3  2  1  1  1  1  1  1  1
  3  1  1  1  1  1  1  1  1  1
  2  2  2  2  2  2
  2  2  2  2  2  1  1
  2  2  2  2  1  1  1  1
  2  2  2  1  1  1  1  1  1
  2  2  1  1  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1
// Kotlin Program for
// Print all distinct combinations of sum x and n natural number
class SumCombination
{
	// Display result
	fun printSequence(result: Array < Int > , k: Int): Unit
	{
		var i: Int = 0;
		while (i < k)
		{
			print("  " + result[i]);
			i += 1;
		}
		print("\n");
	}
	fun findCombination(result: Array < Int > , 
                        index: Int, sum: Int, 
                        x: Int, n: Int): Unit
	{
		if (sum == x)
		{
			// Display calculated result
			this.printSequence(result, index);
			return;
		}
		if (sum > x || index >= x)
		{
			// Base case when stop process
			return;
		}
		var i: Int = n;
		while (i >= 1)
		{
			// Collect result value
			result[index] = i;
			this.findCombination(result, index + 1, sum + i, x, i);
			i -= 1;
		}
	}
	// Handles the request of find combination of given n and x
	fun combination(x: Int, n: Int): Unit
	{
		if (n <= 0 || x <= 0)
		{
			return;
		}
		println("\n Given n : " + n + " x : " + x);
		// Collect result
		val result: Array < Int > = Array(x)
		{
			0
		};
		// Test
		this.findCombination(result, 0, 0, x, n);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: SumCombination = SumCombination();
	// Test A
	// n = 3 means use [1,2,3]
	var n: Int = 3;
	// Sum x = 10
	var x: Int = 10;
	task.combination(x, n);
	// Test B
	// n = 4 means use [1,2,3,4]
	n = 4;
	// Sum x = 12
	x = 12;
	task.combination(x, n);
}

Output

 Given n : 3 x : 10
  3  3  3  1
  3  3  2  2
  3  3  2  1  1
  3  3  1  1  1  1
  3  2  2  2  1
  3  2  2  1  1  1
  3  2  1  1  1  1  1
  3  1  1  1  1  1  1  1
  2  2  2  2  2
  2  2  2  2  1  1
  2  2  2  1  1  1  1
  2  2  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1

 Given n : 4 x : 12
  4  4  4
  4  4  3  1
  4  4  2  2
  4  4  2  1  1
  4  4  1  1  1  1
  4  3  3  2
  4  3  3  1  1
  4  3  2  2  1
  4  3  2  1  1  1
  4  3  1  1  1  1  1
  4  2  2  2  2
  4  2  2  2  1  1
  4  2  2  1  1  1  1
  4  2  1  1  1  1  1  1
  4  1  1  1  1  1  1  1  1
  3  3  3  3
  3  3  3  2  1
  3  3  3  1  1  1
  3  3  2  2  2
  3  3  2  2  1  1
  3  3  2  1  1  1  1
  3  3  1  1  1  1  1  1
  3  2  2  2  2  1
  3  2  2  2  1  1  1
  3  2  2  1  1  1  1  1
  3  2  1  1  1  1  1  1  1
  3  1  1  1  1  1  1  1  1  1
  2  2  2  2  2  2
  2  2  2  2  2  1  1
  2  2  2  2  1  1  1  1
  2  2  2  1  1  1  1  1  1
  2  2  1  1  1  1  1  1  1  1
  2  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1


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







© 2021, kalkicode.com, All rights reserved