Count digits in given number N which divide N | Utility function to check divisibility by digit ; ( N [ i ] - '0' ) gives the digit value and form the number ; Function to count digits which appears in N and divide N divide [ 10 ] -- > array which tells that particular digit divides N or not count [ 10 ] -- > counts frequency of digits which divide N ; We initialize all digits of N as not divisible by N . ; start checking divisibility of N by digits 2 to 9 ; if digit divides N then mark it as true ; Now traverse the number string to find and increment result whenever a digit divides N . ; Driver Code
< ? php function divisible ( $ N , $ digit ) { $ ans = 0 ; for ( $ i = 0 ; $ i < strlen ( $ N ) ; $ i ++ ) { $ ans = ( $ ans * 10 + ( int ) ( $ N [ $ i ] - '0' ) ) ; $ ans %= $ digit ; } return ( $ ans == 0 ) ; } function allDigits ( $ N ) { $ divide = array_fill ( 0 , 10 , false ) ; for ( $ digit = 2 ; $ digit <= 9 ; $ digit ++ ) { if ( divisible ( $ N , $ digit ) ) $ divide [ $ digit ] = true ; } $ result = 0 ; for ( $ i = 0 ; $ i < strlen ( $ N ) ; $ i ++ ) { if ( $ divide [ ( int ) ( $ N [ $ i ] - '0' ) ] == true ) $ result ++ ; } return $ result ; } $ N = "122324" ; echo allDigits ( $ N ) ; ? >