Moser | Function to generate nth term of Moser - de Bruijn Sequence ; 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
< ? php function gen ( $ n ) { $ S = array ( ) ; $ S [ 0 ] = 0 ; $ S [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) $ S [ $ i ] = 4 * $ S [ $ i / 2 ] ; else $ S [ $ i ] = 4 * $ S [ $ i / 2 ] + 1 ; } return $ S [ $ n ] ; } function moserDeBruijn ( $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo gen ( $ i ) , " ▁ " ; echo " STRNEWLINE " ; } $ n = 15 ; echo " First ▁ " , $ n , " ▁ terms ▁ of ▁ " , " Moser - de ▁ Bruijn ▁ Sequence ▁ : ▁ STRNEWLINE " ; moserDeBruijn ( $ n ) ; ? >