Skip to content

Instantly share code, notes, and snippets.

@benjaminaaron
Created May 28, 2018 21:15
Show Gist options
  • Save benjaminaaron/65c7ceecea806331e3687dec0e7ed560 to your computer and use it in GitHub Desktop.
Save benjaminaaron/65c7ceecea806331e3687dec0e7ed560 to your computer and use it in GitHub Desktop.
possible subgroup constellations in a group of size n
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
static List<List<Integer>> partitions = new ArrayList<>();
static void partition(int n, int max, String prefix) {
// from https://introcs.cs.princeton.edu/java/23recursion/Partition.java
if (n == 0) {
partitions.add(Arrays.stream(prefix.trim().split(" ")).map(Integer::valueOf).collect(Collectors.toList()));
return;
}
for (int i = Math.min(max, n); i >= 1; i--) {
partition(n-i, i, prefix + " " + i);
}
}
static long faculty(int val) {
long faculty = 1;
for (int i = 1; i <= val; i ++) {
faculty *= i;
}
return faculty;
}
static long binomialCoefficient(int n, int k) { // n over k
return faculty(n) / (faculty(k) * faculty(n - k));
}
public static void main(String[] args) {
int n = 13; // group of size n
boolean oneMemberPerSubgroup = true; // false means a group member can be in multiple subgroups
partition(n, n, "");
System.out.println(partitions.size() + " subgroup constellations are possible with a group of " + n + ":");
System.out.println(partitions);
long summedOptions = 0;
for (List<Integer> row : partitions) {
long options = 1;
int n_left = n;
for (Integer groupSize : row) {
long binom = binomialCoefficient(n_left, groupSize);
options *= binom;
if (oneMemberPerSubgroup) {
n_left -= groupSize;
}
}
System.out.println(options + " ways to arrange " + n + " in this subgroup constellations: " + row);
summedOptions += options;
}
System.out.println("\n --> " + summedOptions + " ways to create subgroup constellations from a group of " + n
+ " with members" + (oneMemberPerSubgroup ? " NOT " : " ") + "being allowed in more than one subgroup");
}
}
@benjaminaaron
Copy link
Author

benjaminaaron commented May 28, 2018

Shortened output for n = 13 and oneMemberPerSubgroup = true:

101 subgroup constellations are possible with a group of 13

1 ways to arrange 13 in this subgroup constellations: [13]
13 ways to arrange 13 in this subgroup constellations: [12, 1]
78 ways to arrange 13 in this subgroup constellations: [11, 2]
156 ways to arrange 13 in this subgroup constellations: [11, 1, 1]
286 ways to arrange 13 in this subgroup constellations: [10, 3]
858 ways to arrange 13 in this subgroup constellations: [10, 2, 1]
1716 ways to arrange 13 in this subgroup constellations: [10, 1, 1, 1]
715 ways to arrange 13 in this subgroup constellations: [9, 4]
2860 ways to arrange 13 in this subgroup constellations: [9, 3, 1]
4290 ways to arrange 13 in this subgroup constellations: [9, 2, 2]
8580 ways to arrange 13 in this subgroup constellations: [9, 2, 1, 1]
17160 ways to arrange 13 in this subgroup constellations: [9, 1, 1, 1, 1]
1287 ways to arrange 13 in this subgroup constellations: [8, 5]
6435 ways to arrange 13 in this subgroup constellations: [8, 4, 1]
12870 ways to arrange 13 in this subgroup constellations: [8, 3, 2]
25740 ways to arrange 13 in this subgroup constellations: [8, 3, 1, 1]
38610 ways to arrange 13 in this subgroup constellations: [8, 2, 2, 1]
77220 ways to arrange 13 in this subgroup constellations: [8, 2, 1, 1, 1]
154440 ways to arrange 13 in this subgroup constellations: [8, 1, 1, 1, 1, 1]
1716 ways to arrange 13 in this subgroup constellations: [7, 6]
10296 ways to arrange 13 in this subgroup constellations: [7, 5, 1]
25740 ways to arrange 13 in this subgroup constellations: [7, 4, 2]
51480 ways to arrange 13 in this subgroup constellations: [7, 4, 1, 1]
34320 ways to arrange 13 in this subgroup constellations: [7, 3, 3]
102960 ways to arrange 13 in this subgroup constellations: [7, 3, 2, 1]
205920 ways to arrange 13 in this subgroup constellations: [7, 3, 1, 1, 1]
154440 ways to arrange 13 in this subgroup constellations: [7, 2, 2, 2]
308880 ways to arrange 13 in this subgroup constellations: [7, 2, 2, 1, 1]
617760 ways to arrange 13 in this subgroup constellations: [7, 2, 1, 1, 1, 1]
1235520 ways to arrange 13 in this subgroup constellations: [7, 1, 1, 1, 1, 1, 1]
12012 ways to arrange 13 in this subgroup constellations: [6, 6, 1]
36036 ways to arrange 13 in this subgroup constellations: [6, 5, 2]
72072 ways to arrange 13 in this subgroup constellations: [6, 5, 1, 1]
60060 ways to arrange 13 in this subgroup constellations: [6, 4, 3]
180180 ways to arrange 13 in this subgroup constellations: [6, 4, 2, 1]
360360 ways to arrange 13 in this subgroup constellations: [6, 4, 1, 1, 1]
240240 ways to arrange 13 in this subgroup constellations: [6, 3, 3, 1]
360360 ways to arrange 13 in this subgroup constellations: [6, 3, 2, 2]
720720 ways to arrange 13 in this subgroup constellations: [6, 3, 2, 1, 1]
1441440 ways to arrange 13 in this subgroup constellations: [6, 3, 1, 1, 1, 1]
1081080 ways to arrange 13 in this subgroup constellations: [6, 2, 2, 2, 1]
2162160 ways to arrange 13 in this subgroup constellations: [6, 2, 2, 1, 1, 1]
4324320 ways to arrange 13 in this subgroup constellations: [6, 2, 1, 1, 1, 1, 1]
8648640 ways to arrange 13 in this subgroup constellations: [6, 1, 1, 1, 1, 1, 1, 1]
72072 ways to arrange 13 in this subgroup constellations: [5, 5, 3]
216216 ways to arrange 13 in this subgroup constellations: [5, 5, 2, 1]
432432 ways to arrange 13 in this subgroup constellations: [5, 5, 1, 1, 1]
90090 ways to arrange 13 in this subgroup constellations: [5, 4, 4]
360360 ways to arrange 13 in this subgroup constellations: [5, 4, 3, 1]
540540 ways to arrange 13 in this subgroup constellations: [5, 4, 2, 2]
1081080 ways to arrange 13 in this subgroup constellations: [5, 4, 2, 1, 1]
2162160 ways to arrange 13 in this subgroup constellations: [5, 4, 1, 1, 1, 1]
720720 ways to arrange 13 in this subgroup constellations: [5, 3, 3, 2]
1441440 ways to arrange 13 in this subgroup constellations: [5, 3, 3, 1, 1]
2162160 ways to arrange 13 in this subgroup constellations: [5, 3, 2, 2, 1]
4324320 ways to arrange 13 in this subgroup constellations: [5, 3, 2, 1, 1, 1]
8648640 ways to arrange 13 in this subgroup constellations: [5, 3, 1, 1, 1, 1, 1]
3243240 ways to arrange 13 in this subgroup constellations: [5, 2, 2, 2, 2]
6486480 ways to arrange 13 in this subgroup constellations: [5, 2, 2, 2, 1, 1]
12972960 ways to arrange 13 in this subgroup constellations: [5, 2, 2, 1, 1, 1, 1]
25945920 ways to arrange 13 in this subgroup constellations: [5, 2, 1, 1, 1, 1, 1, 1]
51891840 ways to arrange 13 in this subgroup constellations: [5, 1, 1, 1, 1, 1, 1, 1, 1]
450450 ways to arrange 13 in this subgroup constellations: [4, 4, 4, 1]
900900 ways to arrange 13 in this subgroup constellations: [4, 4, 3, 2]
1801800 ways to arrange 13 in this subgroup constellations: [4, 4, 3, 1, 1]
2702700 ways to arrange 13 in this subgroup constellations: [4, 4, 2, 2, 1]
5405400 ways to arrange 13 in this subgroup constellations: [4, 4, 2, 1, 1, 1]
10810800 ways to arrange 13 in this subgroup constellations: [4, 4, 1, 1, 1, 1, 1]
1201200 ways to arrange 13 in this subgroup constellations: [4, 3, 3, 3]
3603600 ways to arrange 13 in this subgroup constellations: [4, 3, 3, 2, 1]
7207200 ways to arrange 13 in this subgroup constellations: [4, 3, 3, 1, 1, 1]
5405400 ways to arrange 13 in this subgroup constellations: [4, 3, 2, 2, 2]
10810800 ways to arrange 13 in this subgroup constellations: [4, 3, 2, 2, 1, 1]
21621600 ways to arrange 13 in this subgroup constellations: [4, 3, 2, 1, 1, 1, 1]
43243200 ways to arrange 13 in this subgroup constellations: [4, 3, 1, 1, 1, 1, 1, 1]
16216200 ways to arrange 13 in this subgroup constellations: [4, 2, 2, 2, 2, 1]
32432400 ways to arrange 13 in this subgroup constellations: [4, 2, 2, 2, 1, 1, 1]
64864800 ways to arrange 13 in this subgroup constellations: [4, 2, 2, 1, 1, 1, 1, 1]
129729600 ways to arrange 13 in this subgroup constellations: [4, 2, 1, 1, 1, 1, 1, 1, 1]
259459200 ways to arrange 13 in this subgroup constellations: [4, 1, 1, 1, 1, 1, 1, 1, 1, 1]
4804800 ways to arrange 13 in this subgroup constellations: [3, 3, 3, 3, 1]
7207200 ways to arrange 13 in this subgroup constellations: [3, 3, 3, 2, 2]
14414400 ways to arrange 13 in this subgroup constellations: [3, 3, 3, 2, 1, 1]
28828800 ways to arrange 13 in this subgroup constellations: [3, 3, 3, 1, 1, 1, 1]
21621600 ways to arrange 13 in this subgroup constellations: [3, 3, 2, 2, 2, 1]
43243200 ways to arrange 13 in this subgroup constellations: [3, 3, 2, 2, 1, 1, 1]
86486400 ways to arrange 13 in this subgroup constellations: [3, 3, 2, 1, 1, 1, 1, 1]
172972800 ways to arrange 13 in this subgroup constellations: [3, 3, 1, 1, 1, 1, 1, 1, 1]
32432400 ways to arrange 13 in this subgroup constellations: [3, 2, 2, 2, 2, 2]
64864800 ways to arrange 13 in this subgroup constellations: [3, 2, 2, 2, 2, 1, 1]
129729600 ways to arrange 13 in this subgroup constellations: [3, 2, 2, 2, 1, 1, 1, 1]
259459200 ways to arrange 13 in this subgroup constellations: [3, 2, 2, 1, 1, 1, 1, 1, 1]
518918400 ways to arrange 13 in this subgroup constellations: [3, 2, 1, 1, 1, 1, 1, 1, 1, 1]
1037836800 ways to arrange 13 in this subgroup constellations: [3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
97297200 ways to arrange 13 in this subgroup constellations: [2, 2, 2, 2, 2, 2, 1]
194594400 ways to arrange 13 in this subgroup constellations: [2, 2, 2, 2, 2, 1, 1, 1]
389188800 ways to arrange 13 in this subgroup constellations: [2, 2, 2, 2, 1, 1, 1, 1, 1]
778377600 ways to arrange 13 in this subgroup constellations: [2, 2, 2, 1, 1, 1, 1, 1, 1, 1]
1556755200 ways to arrange 13 in this subgroup constellations: [2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
3113510400 ways to arrange 13 in this subgroup constellations: [2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
6227020800 ways to arrange 13 in this subgroup constellations: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

--> 15543026747 ways to create subgroup constellations from a group of 13 with members NOT being allowed in more than one subgroup

@benjaminaaron
Copy link
Author

benjaminaaron commented May 28, 2018

Shortened output for n = 13 and oneMemberPerSubgroup = false:

101 subgroup constellations are possible with a group of 13

1 ways to arrange 13 in this subgroup constellations: [13]
169 ways to arrange 13 in this subgroup constellations: [12, 1]
6084 ways to arrange 13 in this subgroup constellations: [11, 2]
13182 ways to arrange 13 in this subgroup constellations: [11, 1, 1]
81796 ways to arrange 13 in this subgroup constellations: [10, 3]
290004 ways to arrange 13 in this subgroup constellations: [10, 2, 1]
628342 ways to arrange 13 in this subgroup constellations: [10, 1, 1, 1]
511225 ways to arrange 13 in this subgroup constellations: [9, 4]
2658370 ways to arrange 13 in this subgroup constellations: [9, 3, 1]
4350060 ways to arrange 13 in this subgroup constellations: [9, 2, 2]
9425130 ways to arrange 13 in this subgroup constellations: [9, 2, 1, 1]
20421115 ways to arrange 13 in this subgroup constellations: [9, 1, 1, 1, 1]
1656369 ways to arrange 13 in this subgroup constellations: [8, 5]
11962665 ways to arrange 13 in this subgroup constellations: [8, 4, 1]
28710396 ways to arrange 13 in this subgroup constellations: [8, 3, 2]
62205858 ways to arrange 13 in this subgroup constellations: [8, 3, 1, 1]
101791404 ways to arrange 13 in this subgroup constellations: [8, 2, 2, 1]
220548042 ways to arrange 13 in this subgroup constellations: [8, 2, 1, 1, 1]
477854091 ways to arrange 13 in this subgroup constellations: [8, 1, 1, 1, 1, 1]
2944656 ways to arrange 13 in this subgroup constellations: [7, 6]
28710396 ways to arrange 13 in this subgroup constellations: [7, 5, 1]
95701320 ways to arrange 13 in this subgroup constellations: [7, 4, 2]
207352860 ways to arrange 13 in this subgroup constellations: [7, 4, 1, 1]
140361936 ways to arrange 13 in this subgroup constellations: [7, 3, 3]
497646864 ways to arrange 13 in this subgroup constellations: [7, 3, 2, 1]
1078234872 ways to arrange 13 in this subgroup constellations: [7, 3, 1, 1, 1]
814331232 ways to arrange 13 in this subgroup constellations: [7, 2, 2, 2]
1764384336 ways to arrange 13 in this subgroup constellations: [7, 2, 2, 1, 1]
3822832728 ways to arrange 13 in this subgroup constellations: [7, 2, 1, 1, 1, 1]
8282804244 ways to arrange 13 in this subgroup constellations: [7, 1, 1, 1, 1, 1, 1]
38280528 ways to arrange 13 in this subgroup constellations: [6, 6, 1]
172262376 ways to arrange 13 in this subgroup constellations: [6, 5, 2]
373235148 ways to arrange 13 in this subgroup constellations: [6, 5, 1, 1]
350904840 ways to arrange 13 in this subgroup constellations: [6, 4, 3]
1244117160 ways to arrange 13 in this subgroup constellations: [6, 4, 2, 1]
2695587180 ways to arrange 13 in this subgroup constellations: [6, 4, 1, 1, 1]
1824705168 ways to arrange 13 in this subgroup constellations: [6, 3, 3, 1]
2985881184 ways to arrange 13 in this subgroup constellations: [6, 3, 2, 2]
6469409232 ways to arrange 13 in this subgroup constellations: [6, 3, 2, 1, 1]
14017053336 ways to arrange 13 in this subgroup constellations: [6, 3, 1, 1, 1, 1]
10586306016 ways to arrange 13 in this subgroup constellations: [6, 2, 2, 2, 1]
22936996368 ways to arrange 13 in this subgroup constellations: [6, 2, 2, 1, 1, 1]
49696825464 ways to arrange 13 in this subgroup constellations: [6, 2, 1, 1, 1, 1, 1]
107676455172 ways to arrange 13 in this subgroup constellations: [6, 1, 1, 1, 1, 1, 1, 1]
473721534 ways to arrange 13 in this subgroup constellations: [5, 5, 3]
1679558166 ways to arrange 13 in this subgroup constellations: [5, 5, 2, 1]
3639042693 ways to arrange 13 in this subgroup constellations: [5, 5, 1, 1, 1]
657946575 ways to arrange 13 in this subgroup constellations: [5, 4, 4]
3421322190 ways to arrange 13 in this subgroup constellations: [5, 4, 3, 1]
5598527220 ways to arrange 13 in this subgroup constellations: [5, 4, 2, 2]
12130142310 ways to arrange 13 in this subgroup constellations: [5, 4, 2, 1, 1]
26281975005 ways to arrange 13 in this subgroup constellations: [5, 4, 1, 1, 1, 1]
8211173256 ways to arrange 13 in this subgroup constellations: [5, 3, 3, 2]
17790875388 ways to arrange 13 in this subgroup constellations: [5, 3, 3, 1, 1]
29112341544 ways to arrange 13 in this subgroup constellations: [5, 3, 2, 2, 1]
63076740012 ways to arrange 13 in this subgroup constellations: [5, 3, 2, 1, 1, 1]
136666270026 ways to arrange 13 in this subgroup constellations: [5, 3, 1, 1, 1, 1, 1]
47638377072 ways to arrange 13 in this subgroup constellations: [5, 2, 2, 2, 2]
103216483656 ways to arrange 13 in this subgroup constellations: [5, 2, 2, 2, 1, 1]
223635714588 ways to arrange 13 in this subgroup constellations: [5, 2, 2, 1, 1, 1, 1]
484544048274 ways to arrange 13 in this subgroup constellations: [5, 2, 1, 1, 1, 1, 1, 1]
1049845437927 ways to arrange 13 in this subgroup constellations: [5, 1, 1, 1, 1, 1, 1, 1, 1]
4751836375 ways to arrange 13 in this subgroup constellations: [4, 4, 4, 1]
11404407300 ways to arrange 13 in this subgroup constellations: [4, 4, 3, 2]
24709549150 ways to arrange 13 in this subgroup constellations: [4, 4, 3, 1, 1]
40433807700 ways to arrange 13 in this subgroup constellations: [4, 4, 2, 2, 1]
87606583350 ways to arrange 13 in this subgroup constellations: [4, 4, 2, 1, 1, 1]
189814263925 ways to arrange 13 in this subgroup constellations: [4, 4, 1, 1, 1, 1, 1]
16726464040 ways to arrange 13 in this subgroup constellations: [4, 3, 3, 3]
59302917960 ways to arrange 13 in this subgroup constellations: [4, 3, 3, 2, 1]
128489655580 ways to arrange 13 in this subgroup constellations: [4, 3, 3, 1, 1, 1]
97041138480 ways to arrange 13 in this subgroup constellations: [4, 3, 2, 2, 2]
210255800040 ways to arrange 13 in this subgroup constellations: [4, 3, 2, 2, 1, 1]
455554233420 ways to arrange 13 in this subgroup constellations: [4, 3, 2, 1, 1, 1, 1]
987034172410 ways to arrange 13 in this subgroup constellations: [4, 3, 1, 1, 1, 1, 1, 1]
344054945520 ways to arrange 13 in this subgroup constellations: [4, 2, 2, 2, 2, 1]
745452381960 ways to arrange 13 in this subgroup constellations: [4, 2, 2, 2, 1, 1, 1]
1615146827580 ways to arrange 13 in this subgroup constellations: [4, 2, 2, 1, 1, 1, 1, 1]
3499484793090 ways to arrange 13 in this subgroup constellations: [4, 2, 1, 1, 1, 1, 1, 1, 1]
7582217051695 ways to arrange 13 in this subgroup constellations: [4, 1, 1, 1, 1, 1, 1, 1, 1, 1]
86977613008 ways to arrange 13 in this subgroup constellations: [3, 3, 3, 3, 1]
142327003104 ways to arrange 13 in this subgroup constellations: [3, 3, 3, 2, 2]
308375173392 ways to arrange 13 in this subgroup constellations: [3, 3, 3, 2, 1, 1]
668146209016 ways to arrange 13 in this subgroup constellations: [3, 3, 3, 1, 1, 1, 1]
504613920096 ways to arrange 13 in this subgroup constellations: [3, 3, 2, 2, 2, 1]
1093330160208 ways to arrange 13 in this subgroup constellations: [3, 3, 2, 2, 1, 1, 1]
2368882013784 ways to arrange 13 in this subgroup constellations: [3, 3, 2, 1, 1, 1, 1, 1]
5132577696532 ways to arrange 13 in this subgroup constellations: [3, 3, 1, 1, 1, 1, 1, 1, 1]
825731869248 ways to arrange 13 in this subgroup constellations: [3, 2, 2, 2, 2, 2]
1789085716704 ways to arrange 13 in this subgroup constellations: [3, 2, 2, 2, 2, 1, 1]
3876352386192 ways to arrange 13 in this subgroup constellations: [3, 2, 2, 2, 1, 1, 1, 1]
8398763503416 ways to arrange 13 in this subgroup constellations: [3, 2, 2, 1, 1, 1, 1, 1, 1]
18197320924068 ways to arrange 13 in this subgroup constellations: [3, 2, 1, 1, 1, 1, 1, 1, 1, 1]
39427528668814 ways to arrange 13 in this subgroup constellations: [3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
2927594809152 ways to arrange 13 in this subgroup constellations: [2, 2, 2, 2, 2, 2, 1]
6343122086496 ways to arrange 13 in this subgroup constellations: [2, 2, 2, 2, 2, 1, 1, 1]
13743431187408 ways to arrange 13 in this subgroup constellations: [2, 2, 2, 2, 1, 1, 1, 1, 1]
29777434239384 ways to arrange 13 in this subgroup constellations: [2, 2, 2, 1, 1, 1, 1, 1, 1, 1]
64517774185332 ways to arrange 13 in this subgroup constellations: [2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
139788510734886 ways to arrange 13 in this subgroup constellations: [2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
302875106592253 ways to arrange 13 in this subgroup constellations: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

--> 661348833658423 ways to create subgroup constellations from a group of 13 with members being allowed in more than one subgroup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment