Generate all unique partitions of a number

Here given code implementation process.

// C Program
// Generate all unique partitions of a 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 == n)
	{
		// Display calculated result
		printSequence(result, index);
		return;
	}
	if (sum > n || index >= n)
	{
		// Base case when stop process
		return;
	}
	for (int i = x; i >= 1; --i)
	{
		// Collect result value
		result[index] = i;
		findCombination(result, index + 1, sum + i, i, n);
	}
}
// Handles the request of find combination of given n
void combination(int n)
{
	if (n <= 0)
	{
		return;
	}
	printf("\n Given n : %d  \n ", n);
	// Collect result
	int result[n];
	// Test
	findCombination(result, 0, 0, n, n);
}
int main()
{
	int n = 6;
	combination(n);
	return 0;
}

Output

 Given n : 6
  6
  5 1
  4 2
  4 1 1
  3 3
  3 2 1
  3 1 1 1
  2 2 2
  2 2 1 1
  2 1 1 1 1
  1 1 1 1 1 1
// Java Program 
// Generate all unique partitions of a number
public class Partition
{
	// 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 == n)
		{
			// Display calculated result
			printSequence(result, index);
			return;
		}
		if (sum > n || index >= n)
		{
			// Base case when stop process
			return;
		}
		for (int i = x; i >= 1; --i)
		{
			// Collect result value
			result[index] = i;
			findCombination(result, index + 1, sum + i, i, n);
		}
	}
	// Handles the request of find combination of given n
	public void combination(int n)
	{
		if (n <= 0)
		{
			return;
		}
		System.out.print("\n Given n : " + n + "\n ");
		// Collect result
		int[] result = new int[n];
		// Test
		findCombination(result, 0, 0, n, n);
	}
	public static void main(String args[])
	{
		Partition task = new Partition();
		int n = 6;
		task.combination(n);
	}
}

Output

 Given n : 6
  6
  5 1
  4 2
  4 1 1
  3 3
  3 2 1
  3 1 1 1
  2 2 2
  2 2 1 1
  2 1 1 1 1
  1 1 1 1 1 1
// Include header file
#include <iostream>
using namespace std;
// C++ Program 
// Generate all unique partitions of a number
class Partition
{
	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 == n)
		{
			// Display calculated result
			this->printSequence(result, index);
			return;
		}
		if (sum > n || index >= n)
		{
			// Base case when stop process
			return;
		}
		for (int i = x; i >= 1; --i)
		{
			// Collect result value
			result[index] = i;
			this->findCombination(result, index + 1, sum + i, i, n);
		}
	}
	// Handles the request of find combination of given n
	void combination(int n)
	{
		if (n <= 0)
		{
			return;
		}
		cout << "\n Given n : " << n << "\n ";
		// Collect result
		int result[n];
		// Test
		this->findCombination(result, 0, 0, n, n);
	}
};
int main()
{
	Partition *task = new Partition();
	int n = 6;
	task->combination(n);
	return 0;
}

Output

 Given n : 6
  6
  5 1
  4 2
  4 1 1
  3 3
  3 2 1
  3 1 1 1
  2 2 2
  2 2 1 1
  2 1 1 1 1
  1 1 1 1 1 1
// Include namespace system
using System;
// Csharp Program 
// Generate all unique partitions of a number
public class Partition
{
	// 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 == n)
		{
			// Display calculated result
			this.printSequence(result, index);
			return;
		}
		if (sum > n || index >= n)
		{
			// Base case when stop process
			return;
		}
		for (int i = x; i >= 1; --i)
		{
			// Collect result value
			result[index] = i;
			this.findCombination(result, index + 1, sum + i, i, n);
		}
	}
	// Handles the request of find combination of given n
	public void combination(int n)
	{
		if (n <= 0)
		{
			return;
		}
		Console.Write("\n Given n : " + n + "\n ");
		// Collect result
		int[] result = new int[n];
		// Test
		this.findCombination(result, 0, 0, n, n);
	}
	public static void Main(String[] args)
	{
		Partition task = new Partition();
		int n = 6;
		task.combination(n);
	}
}

Output

 Given n : 6
  6
  5 1
  4 2
  4 1 1
  3 3
  3 2 1
  3 1 1 1
  2 2 2
  2 2 1 1
  2 1 1 1 1
  1 1 1 1 1 1
package main
import "fmt"
// Go Program 
// Generate all unique partitions of a number
type Partition struct {}
func getPartition() * Partition {
	var me *Partition = &Partition {}
	return me
}
// Display result
func(this Partition) printSequence(result[] int, k int) {
	for i := 0 ; i < k ; i++ {
		fmt.Print(" ", result[i])
	}
	fmt.Print("\n ")
}
func(this Partition) findCombination(result[] int, 
	index int, 
	sum int, 
	x int, 
	n int) {
	if sum == n {
		// Display calculated result
		this.printSequence(result, index)
		return
	}
	if sum > n || index >= n {
		// Base case when stop process
		return
	}
	for i := x ; i >= 1 ; i-- {
		// Collect result value
		result[index] = i
		this.findCombination(result, index + 1, sum + i, i, n)
	}
}
// Handles the request of find combination of given n
func(this Partition) combination(n int) {
	if n <= 0 {
		return
	}
	fmt.Print("\n Given n : ", n, "\n ")
	// Collect result
	var result = make([] int, n)
	// Test
	this.findCombination(result, 0, 0, n, n)
}
func main() {
	var task * Partition = getPartition()
	var n int = 6
	task.combination(n)
}

Output

 Given n : 6
  6
  5 1
  4 2
  4 1 1
  3 3
  3 2 1
  3 1 1 1
  2 2 2
  2 2 1 1
  2 1 1 1 1
  1 1 1 1 1 1
<?php
// Php Program 
// Generate all unique partitions of a number
class Partition
{
	// 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 == $n)
		{
			// Display calculated result
			$this->printSequence($result, $index);
			return;
		}
		if ($sum > $n || $index >= $n)
		{
			// Base case when stop process
			return;
		}
		for ($i = $x; $i >= 1; --$i)
		{
			// Collect result value
			$result[$index] = $i;
			$this->findCombination($result, $index + 1, $sum + $i, $i, $n);
		}
	}
	// Handles the request of find combination of given n
	public	function combination($n)
	{
		if ($n <= 0)
		{
			return;
		}
		echo("\n Given n : ".$n.
			"\n ");
		// Collect result
		$result = array_fill(0, $n, 0);
		// Test
		$this->findCombination($result, 0, 0, $n, $n);
	}
}

function main()
{
	$task = new Partition();
	$n = 6;
	$task->combination($n);
}
main();

Output

 Given n : 6
  6
  5 1
  4 2
  4 1 1
  3 3
  3 2 1
  3 1 1 1
  2 2 2
  2 2 1 1
  2 1 1 1 1
  1 1 1 1 1 1
// Node JS Program 
// Generate all unique partitions of a number
class Partition
{
	// 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 == n)
		{
			// Display calculated result
			this.printSequence(result, index);
			return;
		}
		if (sum > n || index >= n)
		{
			// Base case when stop process
			return;
		}
		for (var i = x; i >= 1; --i)
		{
			// Collect result value
			result[index] = i;
			this.findCombination(result, index + 1, sum + i, i, n);
		}
	}
	// Handles the request of find combination of given n
	combination(n)
	{
		if (n <= 0)
		{
			return;
		}
		process.stdout.write("\n Given n : " + n + "\n ");
		// Collect result
		var result = Array(n).fill(0);
		// Test
		this.findCombination(result, 0, 0, n, n);
	}
}

function main()
{
	var task = new Partition();
	var n = 6;
	task.combination(n);
}
main();

Output

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

def main() :
	task = Partition()
	n = 6
	task.combination(n)

if __name__ == "__main__": main()

Output

 Given n :  6
   6
   5  1
   4  2
   4  1  1
   3  3
   3  2  1
   3  1  1  1
   2  2  2
   2  2  1  1
   2  1  1  1  1
   1  1  1  1  1  1
#  Ruby Program 
#  Generate all unique partitions of a number
class Partition 
	#  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 == n) 
			#  Display calculated result
			self.printSequence(result, index)
			return
		end

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

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

	end

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

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

end

def main() 
	task = Partition.new()
	n = 6
	task.combination(n)
end

main()

Output

 Given n : 6
  6
  5 1
  4 2
  4 1 1
  3 3
  3 2 1
  3 1 1 1
  2 2 2
  2 2 1 1
  2 1 1 1 1
  1 1 1 1 1 1
 
// Scala Program 
// Generate all unique partitions of a number
class Partition()
{
	// 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 == n)
		{
			// Display calculated result
			printSequence(result, index);
			return;
		}
		if (sum > n || index >= n)
		{
			// Base case when stop process
			return;
		}
		var i: Int = x;
		while (i >= 1)
		{
			// Collect result value
			result(index) = i;
			findCombination(result, index + 1, sum + i, i, n);
			i -= 1;
		}
	}
	// Handles the request of find combination of given n
	def combination(n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		print("\n Given n : " + n + "\n ");
		// Collect result
		var result: Array[Int] = Array.fill[Int](n)(0);
		// Test
		findCombination(result, 0, 0, n, n);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Partition = new Partition();
		var n: Int = 6;
		task.combination(n);
	}
}

Output

 Given n : 6
  6
  5 1
  4 2
  4 1 1
  3 3
  3 2 1
  3 1 1 1
  2 2 2
  2 2 1 1
  2 1 1 1 1
  1 1 1 1 1 1
// Swift 4 Program 
// Generate all unique partitions of a number
class Partition
{
	// Display result
	func printSequence(_ result: [Int], _ k: Int)
	{
		var i: Int = 0;
		while (i < k)
		{
			print(" ", result[i], terminator: "");
			i += 1;
		}
		print("\n ", terminator: "");
	}
	func findCombination(_ result: inout[Int], 
      _ index: Int, 
        _ sum: Int, 
          _ x: Int, 
            _ n: Int)
	{
		if (sum == n)
		{
			// Display calculated result
			self.printSequence(result, index);
			return;
		}
		if (sum > n || index >= n)
		{
			// Base case when stop process
			return;
		}
		var i: Int = x;
		while (i >= 1)
		{
			// Collect result value
			result[index] = i;
			self.findCombination(&result, index + 1, sum + i, i, n);
			i -= 1;
		}
	}
	// Handles the request of find combination of given n
	func combination(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		print("\n Given n : ", n ,"\n ", terminator: "");
		// Collect result
		var result: [Int] = Array(repeating: 0, count: n);
		// Test
		self.findCombination(&result, 0, 0, n, n);
	}
}
func main()
{
	let task: Partition = Partition();
	let n: Int = 6;
	task.combination(n);
}
main();

Output

 Given n :  6
   6
   5  1
   4  2
   4  1  1
   3  3
   3  2  1
   3  1  1  1
   2  2  2
   2  2  1  1
   2  1  1  1  1
   1  1  1  1  1  1
// Kotlin Program 
// Generate all unique partitions of a number
class Partition
{
	// 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 == n)
		{
			// Display calculated result
			this.printSequence(result, index);
			return;
		}
		if (sum > n || index >= n)
		{
			// Base case when stop process
			return;
		}
		var i: Int = x;
		while (i >= 1)
		{
			// Collect result value
			result[index] = i;
			this.findCombination(result, 
                                 index + 1, 
                                 sum + i, 
                                 i, 
                                 n);
			i -= 1;
		}
	}
	// Handles the request of find combination of given n
	fun combination(n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		print("\n Given n : " + n + "\n ");
		// Collect result
		val result: Array < Int > = Array(n)
		{
			0
		};
		// Test
		this.findCombination(result, 0, 0, n, n);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Partition = Partition();
	val n: Int = 6;
	task.combination(n);
}

Output

 Given n : 6
  6
  5 1
  4 2
  4 1 1
  3 3
  3 2 1
  3 1 1 1
  2 2 2
  2 2 1 1
  2 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