Created
October 17, 2013 19:47
-
-
Save ylegall/7031073 to your computer and use it in GitHub Desktop.
integer partitions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
import std.algorithm; | |
auto partition(int n) { | |
part(n, [], n); | |
} | |
private void part(int n, int[] array, int start) { | |
if (n == 0) { | |
writeln(array); | |
return; | |
} | |
for (auto i = min(start, n); i >= 1; --i) { | |
part(n - i, array ~ [i], i); | |
} | |
} | |
void main() { | |
partition(6); | |
// [6] | |
// [5, 1] | |
// [4, 2] | |
// [4, 1, 1] | |
// [3, 3] | |
// [3, 2, 1] | |
// [3, 1, 1, 1] | |
// [2, 2, 2] | |
// [2, 2, 1, 1] | |
// [2, 1, 1, 1, 1] | |
// [1, 1, 1, 1, 1, 1] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment