Smallest N digit number with none of its digits as its divisor | Function to calculate power ; Function to check if the N - digit number satisfies the given condition or not ; Getting the last digit ; Every number is divisible by 1 and dividing a number by 0 isn 't possible. Thus  numbers with 0 as a digit  must be discarded. ; If any digit divides the number , return false ; If no digit divides the number , return true ; ; Function to find the smallest number not divisible by any of its digits . ; Get the lower range of n digit number ; Get the high range of n digit number ; check all the N - digit numbers ; If the first number to satify the constraints is found print it , set the flag value and break out of the loop . ; If no such digit found , return - 1 as per problem statement ; Driver code
def power ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT if ( b == 1 ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT tmp = power ( a , b // 2 ) ; NEW_LINE result = tmp * tmp ; NEW_LINE if ( b % 2 == 1 ) : NEW_LINE INDENT result *= a ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT def check ( n ) : NEW_LINE INDENT temp = n ; NEW_LINE while ( temp > 0 ) : NEW_LINE INDENT last_digit = temp % 10 ; NEW_LINE if ( last_digit == 0 or last_digit == 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if ( n % last_digit == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT temp = temp // 10 ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT def solve ( n ) : NEW_LINE INDENT L = power ( 10 , n - 1 ) ; NEW_LINE R = power ( 10 , n ) - 1 ; NEW_LINE flag = 0 ; NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT answer = check ( i ) ; NEW_LINE if ( answer ) : NEW_LINE INDENT print ( i ) NEW_LINE flag += 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 ; NEW_LINE solve ( N ) ; NEW_LINE DEDENT