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
< ? php function gen ( $ n ) { if ( $ n == 0 ) return 0 ; else if ( $ n == 1 ) return 1 ; else if ( $ n % 2 == 0 ) return 4 * gen ( $ n / 2 ) ; else if ( $ n % 2 == 1 ) return 4 * gen ( $ n / 2 ) + 1 ; } 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 " ) ; echo ( moserDeBruijn ( $ n ) ) ; ? >