Skip to content

Instantly share code, notes, and snippets.

@alastairparagas
Last active August 29, 2015 14:17
Show Gist options
  • Save alastairparagas/2d6520b74b959c890dae to your computer and use it in GitHub Desktop.
Save alastairparagas/2d6520b74b959c890dae to your computer and use it in GitHub Desktop.
Amount of bitstrings that are of length 10 and that do not contain "11". There are 144 such possibilities.
/*
Written by: Alastair Paragas
Run the program by copy/pasting this code to http://repl.it/languages/JavaScript.
This program finds how many bitstrings of length 10 does not contain a set of two consecutive 1s
*/
(function (window) {
'use strict';
var binaryRepresentation,
binaryPossibilities = [],
i;
/*
0 in decimal is 0000000000 in binary (smallest bitstring of length 10)
1023 in decimal is 1111111111 in binary (largest bitstring of length 10)
*/
for (i = 0; i <= 1023; i++) {
binaryRepresentation = Number(i).toString(2);
// BitString does not contain two consecutive 1s
if (binaryRepresentation.indexOf('11') === -1) {
binaryPossibilities.push(binaryRepresentation);
}
}
window.console.log("The amount of bitstrings of length 10 that do not contain '11' are "
+ binaryPossibilities.length + ".");
}(window));
// Prevent global namespace pollution through self-executing function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment