Created
September 17, 2012 13:34
-
-
Save viig99/3737299 to your computer and use it in GitHub Desktop.
gremlin_3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
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
This code didn't run for me. Missing a lot of semi-colons. How did you test it?