Generate all unique partitions of a number by iterative approach

Here given code implementation process.

// C Program
// Generate all unique partitions of a number by iterative approach
#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 combination(int n)
{
	if (n <= 0)
	{
		return;
	}
	printf("\n Given n : %d  \n ", n);
	// Collect result
	int result[n];
	int index = 0;
	int temp = 0;
	int count = 0;
	// First value
	result[index] = n;
	while (1)
	{
		// Display result
		printSequence(result, index + 1);
		while (index >= 0 && result[index] == 1)
		{
			temp += result[index];
			index--;
		}
		if (index < 0)
		{
			// Stop process
			return;
		}
		temp += 1;
		// Reduce current element
		result[index] = result[index] - 1;
		while (index < n - 1 && temp > result[index])
		{
			// Set value of next element
			result[index + 1] = result[index];
			// Recude temp value
			temp = temp - result[index];
			// Increase index value
			index++;
		}
		result[index + 1] = temp;
		// Increase index value
		index++;
		// Set 0 for next iteration
		temp = 0;
	}
}
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 by iterative approach
*/
public class Combinations
{
	// 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 combination(int n)
	{
		if (n <= 0)
		{
			return;
		}
		System.out.print("\n Given n : " + n +"\n ") ;
		// Collect result
		int[] result = new int[n];
		int index = 0;
		int temp = 0;
		int count = 0;
		// First value
		result[index] = n;
		while (true)
		{
			// Display result
			printSequence(result, index + 1);

			while (index >= 0 && result[index] == 1)
			{
				temp += result[index];
				index--;
			}
			if (index < 0)
			{
				// Stop process
				return;
			}
			temp += 1;
			// Reduce current element
			result[index] = result[index] - 1;
			while (index < n - 1 && temp > result[index])
			{
				// Set value of next element
				result[index + 1] = result[index];
				// Recude temp value
				temp = temp - result[index];
				// Increase index value
				index++;
			}
			result[index + 1] = temp;
			// Increase index value
			index++;
			// Set 0 for next iteration
			temp = 0;
		}
	}
	public static void main(String[] args)
	{
		Combinations task = new Combinations();
		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 by iterative approach
*/
class Combinations
{
	public:
		// Display result
		void printSequence(int result[], int k)
		{
			for (int i = 0; i < k; ++i)
			{
				cout << " " << result[i];
			}
			cout << "\n ";
		}
	void combination(int n)
	{
		if (n <= 0)
		{
			return;
		}
		cout << "\n Given n : " << n << "\n ";
		// Collect result
		int result[n];
		int index = 0;
		int temp = 0;
		int count = 0;
		// First value
		result[index] = n;
		while (true)
		{
			// Display result
			this->printSequence(result, index + 1);
			while (index >= 0 && result[index] == 1)
			{
				temp += result[index];
				index--;
			}
			if (index < 0)
			{
				// Stop process
				return;
			}
			temp += 1;
			// Reduce current element
			result[index] = result[index] - 1;
			while (index < n - 1 && temp > result[index])
			{
				// Set value of next element
				result[index + 1] = result[index];
				// Recude temp value
				temp = temp - result[index];
				// Increase index value
				index++;
			}
			result[index + 1] = temp;
			// Increase index value
			index++;
			// Set 0 for next iteration
			temp = 0;
		}
	}
};
int main()
{
	Combinations *task = new Combinations();
	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 by iterative approach
*/
public class Combinations
{
	// 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 combination(int n)
	{
		if (n <= 0)
		{
			return;
		}
		Console.Write("\n Given n : " + n + "\n ");
		// Collect result
		int[] result = new int[n];
		int index = 0;
		int temp = 0;
		// First value
		result[index] = n;
		while (true)
		{
			// Display result
			this.printSequence(result, index + 1);
			while (index >= 0 && result[index] == 1)
			{
				temp += result[index];
				index--;
			}
			if (index < 0)
			{
				// Stop process
				return;
			}
			temp += 1;
			// Reduce current element
			result[index] = result[index] - 1;
			while (index < n - 1 && temp > result[index])
			{
				// Set value of next element
				result[index + 1] = result[index];
				// Recude temp value
				temp = temp - result[index];
				// Increase index value
				index++;
			}
			result[index + 1] = temp;
			// Increase index value
			index++;
			// Set 0 for next iteration
			temp = 0;
		}
	}
	public static void Main(String[] args)
	{
		Combinations task = new Combinations();
		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 by iterative approach
*/

// Display result
func printSequence(result[] int, k int) {
	for i := 0 ; i < k ; i++ {
		fmt.Print(" ", result[i])
	}
	fmt.Print("\n ")
}
func combination(n int) {
	if n <= 0 {
		return
	}
	fmt.Print("\n Given n : ", n, "\n ")
	// Collect result
	var result = make([] int, n)
	var index int = 0
	var temp int = 0
	// First value
	result[index] = n
	for (true) {
		// Display result
		printSequence(result, index + 1)
		for (index >= 0 && result[index] == 1) {
			temp += result[index]
			index--
		}
		if index < 0 {
			// Stop process
			return
		}
		temp += 1
		// Reduce current element
		result[index] = result[index] - 1
		for (index < n - 1 && temp > result[index]) {
			// Set value of next element
			result[index + 1] = result[index]
			// Recude temp value
			temp = temp - result[index]
			// Increase index value
			index++
		}
		result[index + 1] = temp
		// Increase index value
		index++
		// Set 0 for next iteration
		temp = 0
	}
}
func main() {

	var n int = 6
	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 by iterative approach
*/
class Combinations
{
	// Display result
	public	function printSequence($result, $k)
	{
		for ($i = 0; $i < $k; ++$i)
		{
			echo(" ".$result[$i]);
		}
		echo("\n ");
	}
	public	function combination($n)
	{
		if ($n <= 0)
		{
			return;
		}
		echo("\n Given n : ".$n."\n ");
		// Collect result
		$result = array_fill(0, $n, 0);
		$index = 0;
		$temp = 0;
		// First value
		$result[$index] = $n;
		while (true)
		{
			// Display result
			$this->printSequence($result, $index + 1);
			while ($index >= 0 && $result[$index] == 1)
			{
				$temp += $result[$index];
				$index--;
			}
			if ($index < 0)
			{
				// Stop process
				return;
			}
			$temp += 1;
			// Reduce current element
			$result[$index] = $result[$index] - 1;
			while ($index < $n - 1 && $temp > $result[$index])
			{
				// Set value of next element
				$result[$index + 1] = $result[$index];
				// Recude temp value
				$temp = $temp - $result[$index];
				// Increase index value
				$index++;
			}
			$result[$index + 1] = $temp;
			// Increase index value
			$index++;
			// Set 0 for next iteration
			$temp = 0;
		}
	}
}

function main()
{
	$task = new Combinations();
	$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 by iterative approach
*/
class Combinations
{
	// Display result
	printSequence(result, k)
	{
		for (var i = 0; i < k; ++i)
		{
			process.stdout.write(" " + result[i]);
		}
		process.stdout.write("\n ");
	}
	combination(n)
	{
		if (n <= 0)
		{
			return;
		}
		process.stdout.write("\n Given n : " + n + "\n ");
		// Collect result
		var result = Array(n).fill(0);
		var index = 0;
		var temp = 0;
		// First value
		result[index] = n;
		while (true)
		{
			// Display result
			this.printSequence(result, index + 1);
			while (index >= 0 && result[index] == 1)
			{
				temp += result[index];
				index--;
			}
			if (index < 0)
			{
				// Stop process
				return;
			}
			temp += 1;
			// Reduce current element
			result[index] = result[index] - 1;
			while (index < n - 1 && temp > result[index])
			{
				// Set value of next element
				result[index + 1] = result[index];
				// Recude temp value
				temp = temp - result[index];
				// Increase index value
				index++;
			}
			result[index + 1] = temp;
			// Increase index value
			index++;
			// Set 0 for next iteration
			temp = 0;
		}
	}
}

function main()
{
	var task = new Combinations();
	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 by iterative approach
class Combinations :
	#  Display result
	def printSequence(self, result, k) :
		i = 0
		while (i < k) :
			print(" ", result[i], end = "")
			i += 1
		
		print("\n ", end = "")
	
	def combination(self, n) :
		if (n <= 0) :
			return
		
		print("\n Given n : ", n ,"\n ", end = "")
		#  Collect result
		result = [0] * (n)
		index = 0
		temp = 0
		#  First value
		result[index] = n
		while (True) :
			#  Display result
			self.printSequence(result, index + 1)
			while (index >= 0 and result[index] == 1) :
				temp += result[index]
				index -= 1
			
			if (index < 0) :
				#  Stop process
				return
			
			temp += 1
			#  Reduce current element
			result[index] = result[index] - 1
			while (index < n - 1 and temp > result[index]) :
				#  Set value of next element
				result[index + 1] = result[index]
				#  Recude temp value
				temp = temp - result[index]
				#  Increase index value
				index += 1
			
			result[index + 1] = temp
			#  Increase index value
			index += 1
			#  Set 0 for next iteration
			temp = 0
		
	

def main() :
	task = Combinations()
	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 by iterative approach
class Combinations 
	#  Display result
	def printSequence(result, k) 
		i = 0
		while (i < k) 
			print(" ", result[i])
			i += 1
		end

		print("\n ")
	end

	def combination(n) 
		if (n <= 0) 
			return
		end

		print("\n Given n : ", n ,"\n ")
		#  Collect result
		result = Array.new(n) {0}
		index = 0
		temp = 0
		#  First value
		result[index] = n
		while (true) 
			#  Display result
			self.printSequence(result, index + 1)
			while (index >= 0 && result[index] == 1) 
				temp += result[index]
				index -= 1
			end

			if (index < 0) 
				#  Stop process
				return
			end

			temp += 1
			#  Reduce current element
			result[index] = result[index] - 1
			while (index < n - 1 && temp > result[index]) 
				#  Set value of next element
				result[index + 1] = result[index]
				#  Recude temp value
				temp = temp - result[index]
				#  Increase index value
				index += 1
			end

			result[index + 1] = temp
			#  Increase index value
			index += 1
			#  Set 0 for next iteration
			temp = 0
		end

	end

end

def main() 
	task = Combinations.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 by iterative approach
*/
class Combinations()
{
	// 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 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);
		var index: Int = 0;
		var temp: Int = 0;
		// First value
		result(index) = n;
		while (true)
		{
			// Display result
			printSequence(result, index + 1);
			while (index >= 0 && result(index) == 1)
			{
				temp += result(index);
				index -= 1;
			}
			if (index < 0)
			{
				// Stop process
				return;
			}
			temp += 1;
			// Reduce current element
			result(index) = result(index) - 1;
			while (index < n - 1 && temp > result(index))
			{
				// Set value of next element
				result(index + 1) = result(index);
				// Recude temp value
				temp = temp - result(index);
				// Increase index value
				index += 1;
			}
			result(index + 1) = temp;
			// Increase index value
			index += 1;
			// Set 0 for next iteration
			temp = 0;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combinations = new Combinations();
		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 by iterative approach
*/
class Combinations
{
	// 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 combination(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		print("\n Given n : ", n ,"\n ", terminator: "");
		// Collect result
		var result: [Int] = Array(repeating: 0, count: n);
		var index: Int = 0;
		var temp: Int = 0;
		// First value
		result[index] = n;
		while (true)
		{
			// Display result
			self.printSequence(result, index + 1);
			while (index >= 0 && result[index] == 1)
			{
				temp += result[index];
				index -= 1;
			}
			if (index < 0)
			{
				// Stop process
				return;
			}
			temp += 1;
			// Reduce current element
			result[index] = result[index] - 1;
			while (index < n - 1 && temp > result[index])
			{
				// Set value of next element
				result[index + 1] = result[index];
				// Recude temp value
				temp = temp - result[index];
				// Increase index value
				index += 1;
			}
			result[index + 1] = temp;
			// Increase index value
			index += 1;
			// Set 0 for next iteration
			temp = 0;
		}
	}
}
func main()
{
	let task: Combinations = Combinations();
	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 by iterative approach
*/
class Combinations
{
	// 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 combination(n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		print("\n Given n : " + n + "\n ");
		// Collect result
		var result: Array < Int > = Array(n)
		{
			0
		};
		var index: Int = 0;
		var temp: Int = 0;
		// First value
		result[index] = n;
		while (true)
		{
			// Display result
			this.printSequence(result, index + 1);
			while (index >= 0 && result[index] == 1)
			{
				temp += result[index];
				index -= 1;
			}
			if (index < 0)
			{
				// Stop process
				return;
			}
			temp += 1;
			// Reduce current element
			result[index] = result[index] - 1;
			while (index < n - 1 && temp > result[index])
			{
				// Set value of next element
				result[index + 1] = result[index];
				// Recude temp value
				temp = temp - result[index];
				// Increase index value
				index += 1;
			}
			result[index + 1] = temp;
			// Increase index value
			index += 1;
			// Set 0 for next iteration
			temp = 0;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combinations = Combinations();
	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