Function to generate nth term of Moser - de Bruijn Sequence
function gen ( n ) { let S = new Array ( n + 1 ) ; S . fill ( 0 ) ; S [ 0 ] = 0 ; if ( n != 0 ) S [ 1 ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) {