
- 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 Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
In this program we will count the number of ways one number can be represented by sum of numbers smaller than itself. This program will count the partition of given numbers. We take a number n as input, then starting from a number break it by removing 1 at a time. If the new partition is generated, increase the counter.
Algorithm
partitionCount(n)
Input : The number n
Output : The number of partitions
Begin Create array p of size n k := 0 count := -1 put n as the first element of array p Repeat the following steps, do increase count by 1 rem := 0 while k >= 0 and p[k] = 1, do rem := rem + p[k] decrease k by 1 done if k < 0, then return count p[k] := p[k] – 1 rem := rem + 1 while rem >= p[k], do p[k+1] := p[k] rem := rem - p[k] increase k by 1 done p[k+1] := rem increase k by 1 done End
Example Code
#include<iostream> using namespace std; int partitionCount(int n){ //used to count all possible partitions int p[n], k = 0, count = -1; p[k] = n; //add n as the first element of array while(true) { //repeat untill all elements are turned to 1 count++; int rem = 0; while (k >= 0 && p[k] == 1){ // Move the pointer to the correct index where p[k] > 1. rem += p[k]; k--; } if (k < 0) // If k < 0 then the all the element are broken down to 1. return count; //otherwise decrease the value by 1, and increase rem p[k]--; rem++; while (rem > p[k]) { // repeat until the number of 1's are greater than the value at k index. p[k+1] = p[k]; rem -= p[k]; // Decrease the rem_val value. k++; } p[k+1] = rem; // Assign remaining value to the index next to k. k++; } } main() { int n, c; cout<<"Enter number for partition counting: "; cin>>n; if (n <= 0) { //n must be greater than or equal to 1 cout<<"Invalid value of n"; exit(1); } c = partitionCount(n); cout<<"The number of partitions: "<<c; }
Output
Enter number for partition counting: 7 The number of partitions: 14
- Related Articles
- Count ways to express a number as sum of consecutive numbers in C++
- Numbers smaller than the current number JavaScript
- Program to find number of ways we can get a number which is sum of nth power of unique numbers in Python
- Write a Golang program to find the sum of digits for a given number
- Find Number of Array Elements Smaller than a Given Number in Java
- Count ways to express a number as sum of powers in C++
- Program to find number of ways where square of number is equal to product of two numbers in Python
- The difference of squares of two numbers is 88. If the larger number is 5 less than twice the smaller number, then find the two numbers.
- C program for a number to be expressed as a sum of two prime numbers.
- The units digit of a two digit number is 3 and seven times the sum of the digits is the number itself. Find the number.
- C Program to Find the minimum sum of factors of a number?
- Program to find number of ways to split a string in Python
- C# program to find the sum of digits of a number using Recursion
- C++ Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Java Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers

Advertisements