Moser | Function to generate nth term of Moser - de Bruijn Sequence ; S ( 0 ) = 0 ; S ( 1 ) = 1 ; S ( 2 * n ) = 4 * S ( n ) ; S ( 2 * n + 1 ) = 4 * S ( n ) + 1 ; Generating the first ' n ' terms of Moser - de Bruijn Sequence ; Driver code
function gen ( n ) { if ( n == 0 ) return 0 ; else if ( n == 1 ) return 1 ; else if ( n % 2 == 0 ) return 4 * gen ( parseInt ( n / 2 , 10 ) ) ; else if ( n % 2 == 1 ) return 4 * gen ( parseInt ( n / 2 , 10 ) ) + 1 ; return 0 ; } function moserDeBruijn ( n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( gen ( i ) + " " ) ; document . write ( " " ) ; } let n = 15 ; document . write ( " " + n + " " + " " + " " ) ; moserDeBruijn ( n ) ;