15
1
I've enjoyed reading this site; this is my first question. Edits are welcome.
Given positive integers n and m, compute all ordered partitions of m into exactly n parts positive integer parts, and print them delimited by commas and newlines. Any order is fine, but each partition must appear exactly once.
For example, given m=6 and n=2, possible partitions are pairs of positive integers that sum to 6:
1,5
2,4
3,3
4,2
5,1
Note that [1,5] and [5,1] are different ordered partitions. Output should be exactly in the format above, with an optional trailing newline. (EDIT: The exact order of the partitions does not matter). Input/output are via standard code-golf I/O.
Another example output for m=7, n=3:
1,1,5
1,2,4
2,1,4
1,3,3
2,2,3
3,1,3
1,4,2
2,3,2
3,2,2
4,1,2
1,5,1
2,4,1
3,3,1
4,2,1
5,1,1
The smallest code in bytes after 1 week wins.
Again, please edit if necessary.
Addendum:
@TimmyD asked what size of integer input the program has to support. There is no hard minimum beyond the examples; indeed, the output size increases exponentially, roughly modeled by: lines = e^(0.6282 n - 1.8273).
n | m | lines of output
2 | 1 | 1
4 | 2 | 2
6 | 3 | 6
8 | 4 | 20
10 | 5 | 70
12 | 6 | 252
14 | 7 | 924
16 | 8 | 3432
18 | 9 | 12870
20 | 10 | 48620
22 | 11 | 184756
24 | 12 | 705432
Do the answers need to support arbitrarily large integers, or is a reasonable upper bound like 2^31-1 suitable? – AdmBorkBork – 2015-12-09T21:05:31.213
4The term "sets" is confusing because order matters. I think the term you're looking for is ordered partitions. – xnor – 2015-12-09T21:11:15.160
2Although it isn't necessary to change, we usually have a looser output format than this. – lirtosiast – 2015-12-09T21:16:41.147
I've changed your wording to allow I/O through function arguments, prompts, and other I/O methods we usually consider acceptable. – lirtosiast – 2015-12-09T21:25:11.650
@TimmyD, the size of the output grows rather explosively so that it's not practical to try m and n past a few hundred, let alone 2^31-1. – cuniculus – 2015-12-09T21:33:43.460
Usually I would expect to be able to just return a list of lists, instead of something with an exact format. Making my Perl 6 follow that makes it 1½ times the size it would otherwise be. – Brad Gilbert b2gills – 2015-12-10T01:04:56.440