Skip to content

Instantly share code, notes, and snippets.

@gartenfeld
Last active August 29, 2015 14:15
Show Gist options
  • Save gartenfeld/7cccfba782bcb161287d to your computer and use it in GitHub Desktop.
Save gartenfeld/7cccfba782bcb161287d to your computer and use it in GitHub Desktop.
Recursive combinatorics.
State of binary switches: 0,0,0,0,0,0
Corresponding combination:
State of binary switches: 0,0,0,0,0,1
Corresponding combination: 100
State of binary switches: 0,0,0,0,1,0
Corresponding combination: 5
State of binary switches: 0,0,0,0,1,1
Corresponding combination: 5,100
State of binary switches: 0,0,0,1,0,0
Corresponding combination: 4
State of binary switches: 0,0,0,1,0,1
Corresponding combination: 4,100
State of binary switches: 0,0,0,1,1,0
Corresponding combination: 4,5
State of binary switches: 0,0,0,1,1,1
Corresponding combination: 4,5,100
State of binary switches: 0,0,1,0,0,0
Corresponding combination: 3
State of binary switches: 0,0,1,0,0,1
Corresponding combination: 3,100
State of binary switches: 0,0,1,0,1,0
Corresponding combination: 3,5
State of binary switches: 0,0,1,0,1,1
Corresponding combination: 3,5,100
State of binary switches: 0,0,1,1,0,0
Corresponding combination: 3,4
State of binary switches: 0,0,1,1,0,1
Corresponding combination: 3,4,100
State of binary switches: 0,0,1,1,1,0
Corresponding combination: 3,4,5
State of binary switches: 0,0,1,1,1,1
Corresponding combination: 3,4,5,100
State of binary switches: 0,1,0,0,0,0
Corresponding combination: 2
State of binary switches: 0,1,0,0,0,1
Corresponding combination: 2,100
State of binary switches: 0,1,0,0,1,0
Corresponding combination: 2,5
State of binary switches: 0,1,0,0,1,1
Corresponding combination: 2,5,100
State of binary switches: 0,1,0,1,0,0
Corresponding combination: 2,4
State of binary switches: 0,1,0,1,0,1
Corresponding combination: 2,4,100
State of binary switches: 0,1,0,1,1,0
Corresponding combination: 2,4,5
State of binary switches: 0,1,0,1,1,1
Corresponding combination: 2,4,5,100
State of binary switches: 0,1,1,0,0,0
Corresponding combination: 2,3
State of binary switches: 0,1,1,0,0,1
Corresponding combination: 2,3,100
State of binary switches: 0,1,1,0,1,0
Corresponding combination: 2,3,5
State of binary switches: 0,1,1,0,1,1
Corresponding combination: 2,3,5,100
State of binary switches: 0,1,1,1,0,0
Corresponding combination: 2,3,4
State of binary switches: 0,1,1,1,0,1
Corresponding combination: 2,3,4,100
State of binary switches: 0,1,1,1,1,0
Corresponding combination: 2,3,4,5
State of binary switches: 0,1,1,1,1,1
Corresponding combination: 2,3,4,5,100
State of binary switches: 1,0,0,0,0,0
Corresponding combination: 1
State of binary switches: 1,0,0,0,0,1
Corresponding combination: 1,100
State of binary switches: 1,0,0,0,1,0
Corresponding combination: 1,5
State of binary switches: 1,0,0,0,1,1
Corresponding combination: 1,5,100
State of binary switches: 1,0,0,1,0,0
Corresponding combination: 1,4
State of binary switches: 1,0,0,1,0,1
Corresponding combination: 1,4,100
State of binary switches: 1,0,0,1,1,0
Corresponding combination: 1,4,5
State of binary switches: 1,0,0,1,1,1
Corresponding combination: 1,4,5,100
State of binary switches: 1,0,1,0,0,0
Corresponding combination: 1,3
State of binary switches: 1,0,1,0,0,1
Corresponding combination: 1,3,100
State of binary switches: 1,0,1,0,1,0
Corresponding combination: 1,3,5
State of binary switches: 1,0,1,0,1,1
Corresponding combination: 1,3,5,100
State of binary switches: 1,0,1,1,0,0
Corresponding combination: 1,3,4
State of binary switches: 1,0,1,1,0,1
Corresponding combination: 1,3,4,100
State of binary switches: 1,0,1,1,1,0
Corresponding combination: 1,3,4,5
State of binary switches: 1,0,1,1,1,1
Corresponding combination: 1,3,4,5,100
State of binary switches: 1,1,0,0,0,0
Corresponding combination: 1,2
State of binary switches: 1,1,0,0,0,1
Corresponding combination: 1,2,100
State of binary switches: 1,1,0,0,1,0
Corresponding combination: 1,2,5
State of binary switches: 1,1,0,0,1,1
Corresponding combination: 1,2,5,100
State of binary switches: 1,1,0,1,0,0
Corresponding combination: 1,2,4
State of binary switches: 1,1,0,1,0,1
Corresponding combination: 1,2,4,100
State of binary switches: 1,1,0,1,1,0
Corresponding combination: 1,2,4,5
State of binary switches: 1,1,0,1,1,1
Corresponding combination: 1,2,4,5,100
State of binary switches: 1,1,1,0,0,0
Corresponding combination: 1,2,3
State of binary switches: 1,1,1,0,0,1
Corresponding combination: 1,2,3,100
State of binary switches: 1,1,1,0,1,0
Corresponding combination: 1,2,3,5
State of binary switches: 1,1,1,0,1,1
Corresponding combination: 1,2,3,5,100
State of binary switches: 1,1,1,1,0,0
Corresponding combination: 1,2,3,4
State of binary switches: 1,1,1,1,0,1
Corresponding combination: 1,2,3,4,100
State of binary switches: 1,1,1,1,1,0
Corresponding combination: 1,2,3,4,5
State of binary switches: 1,1,1,1,1,1
Corresponding combination: 1,2,3,4,5,100
var array = [1,2,3,4,5,100],
arrayEnd = array.length - 1,
on = [];
var printComb = function (arr, switches) {
var subSet = [];
for (var i=0; i<arr.length; i++) {
if (switches[i] === 1) {
subSet.push(arr[i]);
}
}
return subSet;
};
var layer = function (level) {
for (var b = 0; b < 2; b++) {
on[level] = b;
if (level===arrayEnd) {
// at the onion's core:
console.log("State of binary switches: \t" + on);
console.log("Corresponding combination: \t" + printComb(array, on));
} else {
// if core hasn't been reached
// call another layer
layer(level+1);
}
}
};
layer(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment