Find nth number that contains the digit k or divisible by k . | Function for checking if digit k is in n or not ; finding remainder ; if digit found ; Function for finding nth number ; since k is the first which satisfy the criteria , so consider it in count making count = 1 and starting from i = k + 1 ; checking that the number contain k digit or divisible by k ; Driver code
< ? php function checkdigit ( $ n , $ k ) { while ( $ n ) { $ rem = $ n % 10 ; if ( $ rem == $ k ) return 1 ; $ n = $ n / 10 ; } return 0 ; } function findNthNumber ( $ n , $ k ) { for ( $ i = $ k + 1 , $ count = 1 ; $ count < $ n ; $ i ++ ) { if ( checkdigit ( $ i , $ k ) || ( $ i % $ k == 0 ) ) $ count ++ ; if ( $ count == $ n ) return $ i ; } return -1 ; } $ n = 10 ; $ k = 2 ; echo findNthNumber ( $ n , $ k ) ; ? >