Count divisors of n that have at | C ++ 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 program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDigitPresent ( int m , bool hash [ ] ) { while ( m ) { if ( hash [ m % 10 ] ) return true ; m = m / 10 ; } return false ; } int countDivisibles ( int n ) { bool hash [ 10 ] = { 0 } ; int m = n ; while ( m ) { hash [ m % 10 ] = true ; m = m / 10 ; } int ans = 0 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( isDigitPresent ( i , hash ) ) ans ++ ; if ( n / i != i ) { if ( isDigitPresent ( n / i , hash ) ) ans ++ ; } } } return ans ; } int main ( ) { int n = 15 ; cout << countDivisibles ( n ) ; return 0 ; }