Skip to content

Instantly share code, notes, and snippets.

@ylegall
Created October 17, 2013 19:47
Show Gist options
  • Save ylegall/7031073 to your computer and use it in GitHub Desktop.
Save ylegall/7031073 to your computer and use it in GitHub Desktop.
integer partitions.
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