Generate all possible permutations of a Number divisible by N | Function to generate all permutations and print the ones that are divisible by the N ; Convert string to integer ; Check for divisibility and print it ; Print all the permutations ; Swap characters ; Permute remaining characters ; Revoke the swaps ; Driver Code
def permute ( st , l , r , n ) : NEW_LINE INDENT if ( l == r ) : NEW_LINE INDENT p = ' ' . join ( st ) NEW_LINE j = int ( p ) NEW_LINE if ( j % n == 0 ) : NEW_LINE INDENT print ( p ) NEW_LINE DEDENT return NEW_LINE DEDENT for i in range ( l , r ) : NEW_LINE INDENT st [ l ] , st [ i ] = st [ i ] , st [ l ] NEW_LINE permute ( st , l + 1 , r , n ) NEW_LINE st [ l ] , st [ i ] = st [ i ] , st [ l ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = "125" NEW_LINE n = 5 NEW_LINE length = len ( st ) NEW_LINE if ( length > 0 ) : NEW_LINE p = list ( st ) NEW_LINE permute ( p , 0 , length , n ) ; NEW_LINE DEDENT