It is rarely a good idea to tell a story from beginning to end. There are, of course, exceptions, as George Lucas has made it a point to demonstrate with Star Wars and Indiana Jones. Following his example, this story starts somewhere in the middle, with:

Q=function(n){

s=0,c=(1<<n)-1,

f=function(l,o,r,c){

var v=~(l|o|r)&c;

while(v>0){

var t=-v&v;

v=v^t;f((l|t)<<1,(o|t),(r|t)>>1,c)

}

o==c&&s++

};

f(0,0,0,c);

return s

}

The one hundred and fifty four characters above make up a bitwise backtracking solution to the algorithmic puzzle known as n-queens. They weren't written for legibility in this form, but in a *failed attempt to fit a blazing fast javascript solution to a spectacularly complex computer science problem into a tweet. Every semicolon in the code is essential. Every space between lines is discardable. It fits in a text message.

Q=function(n){s=0,c=(1<<n)-1,f=function(l,o,r,c){var v=~(l|o|r)&c;while(v>0){var t=-v&v;v=v^t;f((l|t)<<1,(o|t),(r|t)>>1,c)}o==c&&s++};f(0,0,0,c);return s}

These lines of code are the product of an exhilarating four hour sprint during which my pair, John S. Dvorak and I wrapped our head around bitwise operators, attempting to design and write a solution to n-queens. What is n-queens?

- Imagine a 'chess-like' board, of size n x n, where n is any positive integer
- A solution is defined as a configuration n queens on the chessboard, placed such that they are unable to attack each other.
- An algorithm to solve n-queens for a board size n must determine how many solutions exist for a given n.

It takes a moment for the computational intensity of the problem to sink in at first glance. The number of spots on a board is n squared. The number of possible configurations of n queens on an n x n sized board is n squared choose n. On an 8 x 8 board, that works out to 4,426,165,368 possibilities. In that problem space there are only 92 solutions.

So how long does it take the algorithm John and I wrote to tackle that problem space and report back that there are 92 solutions? On our Mac workstations at Hack Reactor, less than a millisecond. What about a 10 x 10 board, with 17.3 trillion possibilities? How long do we take to return the 724 problems in this case? Also about a millisecond. It gets a little slower from there on out. To find the 2,680 solutions on a 11 x 11 board takes a whole 5 milliseconds. We were forgiving, at first. After all, 11 x 11 is a 1.27 quadrillion position problem space.

As enchanted as we remain by our n-queens solution, and the things we can do with it, both John and I know that we could not have done this without at least a little help from circumstance. It all started with a Sieve of Atkin.

*To be continued. *