I have a program that tells me different ways that a number can be added up like so:
1 2 3 4 5 6 7 8
Given the number 5:
4 1
3 2
3 1 1
2 2 1
2 1 1 1
1 1 1 1 1
The program tells me how many ways the number 5 can be added up by simply counting the lines of code printed. But what if I want to count the ways in which the same numbers exist, but in different order. For example:
1 2 3 4 5 6 7 8
Given the number 5:
4 1 AND 1 4
3 2 AND 2 3
3 1 1 AND 1 1 3 AND 1 3 1
2 2 1 AND 1 2 1 AND 1 2 2
2 1 1 1 AND 1 1 1 2 AND 1 1 2 1 AND 1 2 1 1
1 1 1 1 1
I can tell the number of ways it can be arranged.....If you want to print all the ways , I dont really see the point.....
Consider n balls where n represents your number...
n=5:
b b b b b
first this can be represented either as a sum of 2 numbers ,3 nos ,4 nos and 5 nos.
consider the gaps between the numbers.
you can either place a stick in the gap or not place a stick in the gap ( 1 / 0 ).
When u place the stick it divides the number into any of the above sum possible.
b (0/1) b (0/1) b (0/1) b (0/1) b.
eg:
b 0 b 1 b 0 b 1 b
would represent 2 2 1.
so there are 2^4 - 1 ways ( it cannot be 0 0 0 0 as it means that no stick is placed )
In general 2^(n-1) - 1.
The general formula was pretty much what I was looking for, thanks! But would a more efficient way of accomplishing this be through recursion? Would it be possible to solve this problem using recursion?