Find the n | Function to return the nth string in the required sequence ; Length of the resultant string ; Relative index ; Initial string of length len consists of all a 's since the  list is sorted ; Convert relative index to Binary form and set 0 = a and 1 = b ; Reverse and return the string ; Driver Code
< ? php function obtain_str ( $ n ) { $ len = ( int ) ( log ( $ n + 1 ) / log ( 2 ) ) ; $ rel_ind = $ n + 1 - pow ( 2 , $ len ) ; $ i = 0 ; $ str = " " ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ str . = ' a ' ; } $ i = 0 ; while ( $ rel_ind > 0 ) { if ( $ rel_ind % 2 == 1 ) $ str [ $ i ] = ' b ' ; $ rel_ind = ( int ) ( $ rel_ind / 2 ) ; $ i ++ ; } return strrev ( $ str ) ; } $ n = 11 ; echo obtain_str ( $ n ) ; ? >