Smallest integer with digit sum M and multiple of N | Function to return digit sum ; Function to find out the smallest integer ; Start of the iterator ( Smallest multiple of n ) ; Driver code
< ? php function digitSum ( $ n ) { $ ans = 0 ; while ( $ n ) { $ ans += $ n % 10 ; $ n /= 10 ; } return $ ans ; } function findInt ( $ n , $ m ) { $ minDigit = floor ( $ m / 9 ) ; $ start = pow ( 10 , $ minDigit ) - ( int ) pow ( 10 , $ minDigit ) % $ n ; while ( $ start < PHP_INT_MAX ) { if ( digitSum ( $ start ) == $ m ) return $ start ; else $ start += $ n ; } return -1 ; } $ n = 13 ; $ m = 32 ; echo findInt ( $ n , $ m ) ; # This  code is contributed by ajit. NEW_LINE ? >