Created
April 25, 2018 18:31
-
-
Save rd13/917a4c7b297cf4a72dbdac00d5a01134 to your computer and use it in GitHub Desktop.
Javascript cartesian product (power set) using bits
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
const input = 'abc' | |
const inputLength = 3 | |
const powerSetSize = Math.pow(2, inputLength) | |
let result = [] | |
for (let k = 0; k < powerSetSize; k++) { | |
let set = ""; | |
let binaryDigits = k; | |
for (let j = 0; j < inputLength; j++) { | |
if (binaryDigits % 2 == 1) { | |
set += input.charAt(j); | |
} | |
binaryDigits >>= 1; | |
} | |
result[k] = set; | |
} | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment