
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to perform unique factorization of a Given Number
Here is a C++ Program to get all the unique factorization of a given integer such that addition of a partition results an integer. In this program, a positive integer n is given, and we shall generate all possible unique ways to represent n as sum of positive integers.
Algorithm
Begin function displayAllUniqueParts(int m): 1) Set Index of last element k in a partition to 0 2) Initialize first partition as number itself, p[k]=m 3) Create a while loop which first prints current partition, then generates next partition. The loop stops when the current partition has all 1s. 4) Display current partition as displayArray(p, k + 1) 5) Generate next partition: 6) Initialize val = 0. Find the rightmost non-one value in p[]. Also, update the val so that we know how much value can be accommodated. If k < 0, all the values are 1 so there are no more partitions Decrease the p[k] found above and adjust the val. 7) If val is more, then the sorted order is violated. Divide val in different values of size p[k] and copy these values at different positions after p[k]. Copy val to next position and increment position. End
Example
#include<iostream> using namespace std; void displayArray(int p[], int m) //to print the array { for (int i = 0; i < m; i++) cout << p[i] << " "; cout << endl; } void displayAllUniqueParts(int m) { int p[m]; int k = 0; p[k] = m; while (true) { displayArray(p, k + 1); int val = 0; //initialize val while (k >= 0 && p[k] == 1) { val += p[k]; //update val k--; } if (k < 0) return; p[k]--; val++; while (val > p[k]) //if val is more { p[k + 1] = p[k]; val = val - p[k]; k++; } p[k + 1] = val; k++; } } int main() { cout << "Display All Unique Partitions of 3\n"; displayAllUniqueParts(3); cout << "\nDisplay All Unique Partitions of 4\n"; displayAllUniqueParts(4); cout << "\nDisplay All Unique Partitions of 5\n"; displayAllUniqueParts(5); return 0; }
Output
Display All Unique Partitions of 3 3 2 1 1 1 1 Display All Unique Partitions of 4 4 3 1 2 2 2 1 1 1 1 1 1 Display All Unique Partitions of 5 5 4 1 3 2 3 1 1 2 2 1 2 1 1 1 1 1 1 1 1
- Related Articles
- Program to count number of unique paths that includes given edges in Python
- Java Program to find Product of unique prime factors of a number
- Program to count number of unique subsequences of a string in C++
- C++ Program to Perform Preorder Recursive Traversal of a Given Binary Tree
- C++ Program to Perform Inorder Recursive Traversal of a Given Binary Tree
- C++ Program to Perform Postorder Recursive Traversal of a Given Binary Tree
- C++ Program to Perform Complex Number Multiplication
- C/C++ Program to find Product of unique prime factors of a number?
- Program to perform given operation with each element of a list and given value in Python
- C++ Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
- C++ Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
- C++ Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
- Product of unique prime factors of a number in Python Program
- Python Program for Product of unique prime factors of a number
- Find the smallest sum of all indices of unique number pairs summing to a given number in JavaScript

Advertisements