javascript - Generating all possible binary combinations in words ('true' 'false') performance -
I see the performance improvement of the script below. I am pretty sure that it can be modified to a great extent, I did it because it was the first thing that came to my head and it is only for the performance which I am seeing.
function pad Nn, width, z) {z = z || '0'; Nn = nn + ''; Return nn.length & gt; = Width? Nn: new array (width - nn.length + 1) .join (z) + nn; } Var makeIntoBinary = function (ii, length) {return pad (ii.toString (2), length); } Make makeIntoTrueFalse = function (binary) {var str = ''; (Iii = 0; iii & lt; binary.length; iii ++) for (if (binary [iii] == '0') {str + = 'false';} Else {str + = 'true'; }}; Console.log (str + "+ + binary);} Var runner = function {n} {var iter = Math.pow (2, n); for (i = 0; i ; iter; i ++) {makeIntoTrueFalse (MTN)}}}
I want to generate a set of words for all possible combinations which are basically binary above (< Code> Runner (2); false false
, false true
, true false
, will generate true true
) I'm seeing fast algorithms of electricity Which takes me to this point.
Try to manipulate without bit string conversion, Directly.
Function Combination (n) {var r = []; for (var i = 0; i & lt; (1 & lt; & lt; n); I ++) {var c = []; For (var j = 0; j & lt; n; j ++) {c.push (i and (1 & lt; & lt; j) 'true': 'wrong'); } R.push (c.join ('')); } Return R; } R = combination (prompt ('size?')); Document.write (JSON.stringify (r)); For the record, this is probably slower, but the path is good: number. Protopp Times = function (fn) {var r = []; For (var i = 0; i
Comments
Post a Comment