Given a natural number n, find the number of ways in which n can be expressed as a sum of natural numbers when order is taken into consideration. Two sequences that differ in the order of their terms define different compositions of their sum.

Examples:

Input : 4 Output : 8 Explanation All 8 position composition are: 4, 1+3, 3+1, 2+2, 1+1+2, 1+2+1, 2+1+1 and 1+1+1+1 Input : 8 Output : 128

A Simple Solution is to generate all compositions and count them.

Using the concept of combinatorics, it can be proved that any natural number n will have **2^(n-1) distinct compositions** when order is taken into consideration.

One way to see why the answer is 2^(n-1) directly is to write n as a sum of 1s:

n = 1 + 1 + 1 +…+ 1 (n times).

There are (n-1) plus signs between all 1s. For every plus sign we can choose to split ( by putting a bracket) at the point or not split. Therefore answer is 2^(n-1).

For example, n = 4

No Split

4 = 1 + 1 + 1 + 1 [We write as single 4]Different ways to split once

4 = (1) + (1 + 1 + 1) [We write as 1 + 3]

4 = (1 + 1) + (1 + 1) [We write as 2 + 2]

4 = (1 + 1 + 1) + (1) [We write as 3 + 1]Different ways to split twice

4 = (1) + (1 + 1) + (1) [We write as 1 + 2 + 1]

4 = (1 + 1) + (1) + (1) [We write as 2 + 1 + 1]

4 = (1) + (1) + (1 + 1) [We write as 1 + 1 + 2]Different ways to split three times

4 = (1) + (1) + (1) + (1) [We write as 1 + 1 + 1 + 1]Since there are

(n-1)plus signs between the n 1s, there are 2^(n-1) ways of choosing where to split the sum, and hence 2^(n-1) possible sums .

## C++

`// C++ program to find the total number of ` `// compositions of a natural number ` `#include<iostream> ` `using` `namespace` `std; ` ` ` `#define ull unsigned long long ` ` ` `ull countCompositions(ull n) ` `{ ` ` ` `// Return 2 raised to power (n-1) ` ` ` `return` `(1L) << (n-1); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `ull n = 4; ` ` ` `cout << countCompositions(n) << ` `"\n"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find ` `// the total number of ` `// compositions of a ` `// natural number ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` `public` `static` `int` `countCompositions(` `int` `n) ` `{ ` ` ` `// Return 2 raised ` ` ` `// to power (n-1) ` ` ` `return` `1` `<< (n - ` `1` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `n = ` `4` `; ` ` ` `System.out.print(countCompositions(n)); ` `} ` `} ` ` ` `// This code is contributed by ` `// Akanksha Rai(Abby_akku) ` |

*chevron_right*

*filter_none*

## Python

`# Python code to find the total number of ` `# compositions of a natural number ` `def` `countCompositions(n): ` ` ` ` ` `# function to return the total number ` ` ` `# of composition of n ` ` ` `return` `(` `2` `*` `*` `(n` `-` `1` `)) ` ` ` `# Driver Code ` `print` `(countCompositions(` `4` `)) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the ` `// total number of compositions ` `// of a natural number ` `using` `System; ` ` ` `class` `GFG ` `{ ` `public` `static` `int` `countCompositions(` `int` `n) ` `{ ` ` ` `// Return 2 raised ` ` ` `// to power (n-1) ` ` ` `return` `1 << (n - 1); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 4; ` ` ` `Console.Write(countCompositions(n)); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the ` `// total number of compositions ` `// of a natural number ` ` ` `function` `countCompositions(` `$n` `) ` `{ ` ` ` `// Return 2 raised ` ` ` `// to power (n-1) ` ` ` `return` `((1) << (` `$n` `- 1)); ` `} ` ` ` `// Driver Code ` `$n` `= 4; ` `echo` `countCompositions(` `$n` `), ` `"\n"` `; ` ` ` `// This code is contributed ` `// by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

8

This article is contributed by **Sruti Rai **. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of all proper divisors of a natural number
- Smallest number to multiply to convert floating point to natural
- Find sum of modulo K of first N natural number
- Find the first natural number whose factorial is divisible by x
- Find sum of divisors of all the divisors of a natural number
- Count pairs of natural numbers with GCD equal to given number
- Largest natural number that can be printed with M characters
- Greatest divisor which divides all natural number in range [L, R]
- Number of distinct prime factors of first n natural numbers
- Sum of first N natural numbers by taking powers of 2 as negative number
- Find the k-th smallest divisor of a natural number N
- Expressing a fraction as a natural number under modulo 'm'
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Partition first N natural number into two sets such that their sum is not coprime
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Number of valid indices in the permutation of first N natural numbers
- Find all Factors of Large Perfect Square Natural Number in O(sqrt(sqrt(N))
- Find the Nth natural number which is not divisible by A
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N