Skip to content

Instantly share code, notes, and snippets.

@viig99
Created September 17, 2012 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viig99/3737299 to your computer and use it in GitHub Desktop.
Save viig99/3737299 to your computer and use it in GitHub Desktop.
gremlin_3
var arr = [3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99]
//var arr = [1,2,3,4,6]
function powerset(ary) {
var ps = [[]];
for (var i=0; i < ary.length; i++) {
for (var j = 0, len = ps.length; j < len; j++) {
ps.push(ps[j].concat(ary[i]));
}
}
return ps;
}
sets = powerset(arr)
count = 0
sets.forEach(function(node) {
var max = Math.max.apply(Math, node)
var rest = node.reduce(function(a,b) {return a + b},0) - max;
if (max == rest)
count++
})
console.log(count)
@jjosef
Copy link

jjosef commented Sep 17, 2012

This code didn't run for me. Missing a lot of semi-colons. How did you test it?

@viig99
Copy link
Author

viig99 commented Sep 17, 2012

Yup it's run alright, i ran it on node, still takes a good 12.938 secs to run, probably will crash the browser :D
there are 4194304 sub sets in the power set :P

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