Skip to content

Instantly share code, notes, and snippets.

@evan-goode
Last active December 9, 2015 04:39
Show Gist options
  • Save evan-goode/86dff6918bd7314cce91 to your computer and use it in GitHub Desktop.
Save evan-goode/86dff6918bd7314cce91 to your computer and use it in GitHub Desktop.
function or(array) {
var probability = 0
var combos = []
for(var i = 1; i < array.length + 1; i++) {
var combinations = getCombinations(array, i)
for(var j = 0; j < combinations.length; j++) {
combos.push(combinations[j])
}
}
for(var i = 0; i < combos.length; i++) {
product = combos[i][0]
for(var j = 1; j < combos[i].length; j++) {
product *= combos[i][j]
}
if(combos[i].length % 2 == 0) {
probability -= product
} else {
probability += product
}
}
return probability
}
function getCombinations(array, k) {
if(k > array.length || k <= 0) {
return []
}
if(k == array.length) {
return [array]
}
if(k == 1) {
var combinations = []
for(i = 0; i < array.length; i++) {
combinations.push([array[i]]);
}
return combinations;
}
combinations = [];
for(var i = 0; i < array.length - k + 1; i++) {
var head = array.slice(i, i + 1);
var tail = getCombinations(array.slice(i + 1), k - 1);
for(var j = 0; j < tail.length; j++) {
combinations.push(head.concat(tail[j]));
}
}
return combinations;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment