Count divisors of n that have at | Python3 program to count divisors of n that have at least one digit common with n ; Function to return true if any digit of m is present in hash [ ] . ; check till last digit ; if number is also present in original number then return true ; if no number matches then return 1 ; Count the no of divisors that have at least 1 digits same ; Store digits present in n in a hash [ ] ; marks that the number is present ; last digit removed ; loop to traverse from 1 to sqrt ( n ) to count divisors ; if i is the factor ; call the function to check if any digits match or not ; if n / i != i then a different number , then check it also ; return the answer ; Driver Code
import math NEW_LINE def isDigitPresent ( m , Hash ) : NEW_LINE INDENT while ( m ) : NEW_LINE INDENT if ( Hash [ m % 10 ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT m = m // 10 NEW_LINE DEDENT return False NEW_LINE DEDENT def countDivisibles ( n ) : NEW_LINE INDENT Hash = [ False for i in range ( 10 ) ] NEW_LINE m = n NEW_LINE while ( m ) : NEW_LINE INDENT Hash [ m % 10 ] = True NEW_LINE m = m // 10 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 1 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( isDigitPresent ( i , Hash ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT if ( n // i != i ) : NEW_LINE INDENT if ( isDigitPresent ( n // i , Hash ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT n = 15 NEW_LINE print ( countDivisibles ( n ) ) NEW_LINE