Nth character in Concatenated Decimal String | Method to get dth digit of number N ; Method to return Nth character in concatenated decimal string ; sum will store character escaped till now ; dist will store numbers escaped till now ; loop for number lengths ; nine * len will be incremented characters and nine will be incremented numbers ; restore variables to previous correct state ; get distance from last one digit less maximum number ; d will store dth digit of current number ; method will return dth numbered digit of ( dist + diff ) number ; Driver code to test above methods
def getDigit ( N , d ) : NEW_LINE INDENT string = str ( N ) NEW_LINE return string [ d - 1 ] ; NEW_LINE DEDENT def getNthChar ( N ) : NEW_LINE INDENT sum = 0 NEW_LINE nine = 9 NEW_LINE dist = 0 NEW_LINE for len in range ( 1 , N ) : NEW_LINE INDENT sum += nine * len NEW_LINE dist += nine NEW_LINE if ( sum >= N ) : NEW_LINE INDENT sum -= nine * len NEW_LINE dist -= nine NEW_LINE N -= sum NEW_LINE break NEW_LINE DEDENT nine *= 10 NEW_LINE DEDENT diff = ( N / len ) + 1 NEW_LINE d = N % len NEW_LINE if ( d == 0 ) : NEW_LINE INDENT d = len NEW_LINE DEDENT return getDigit ( dist + diff , d ) ; NEW_LINE DEDENT N = 251 NEW_LINE print getNthChar ( N ) NEW_LINE