Finding the Nth term in a sequence formed by removing digit K from natural numbers | Python 3 implementation for the above approach ; Denotes the digit place ; Method to convert any number to binary equivalent ; denotes the current digits place ; If current digit is >= K increment its value by 1 ; Else add the digit as it is ; Move to the next digit ; Driver code
def convertToBase9 ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE a = 1 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT ans += ( a * ( n % 9 ) ) NEW_LINE a *= 10 NEW_LINE n //= 9 NEW_LINE DEDENT return ans NEW_LINE DEDENT def getNthnumber ( base9 , K ) : NEW_LINE INDENT ans = 0 NEW_LINE a = 1 NEW_LINE while ( base9 > 0 ) : NEW_LINE INDENT cur = base9 % 10 NEW_LINE if ( cur >= K ) : NEW_LINE INDENT ans += a * ( cur + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT ans += a * cur NEW_LINE DEDENT base9 //= 10 NEW_LINE a *= 10 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 NEW_LINE K = 1 NEW_LINE base9 = convertToBase9 ( N ) NEW_LINE print ( getNthnumber ( base9 , K ) ) NEW_LINE DEDENT