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
def gen ( n ) : NEW_LINE INDENT S = [ 0 , 1 ] NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if i % 2 == 0 : NEW_LINE INDENT S . append ( 4 * S [ int ( i / 2 ) ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT S . append ( 4 * S [ int ( i / 2 ) ] + 1 ) ; NEW_LINE DEDENT DEDENT z = S [ n ] ; NEW_LINE return z ; NEW_LINE DEDENT def moserDeBruijn ( n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( gen ( i ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 15 NEW_LINE print ( " First " , n , " terms ▁ of ▁ " , " Moser - de ▁ Brujn ▁ Sequence : " ) NEW_LINE moserDeBruijn ( n ) NEW_LINE