Steps to reduce N to zero by subtracting its most significant digit at every step | Function to count the number of digits in a number m ; Function to count the number of steps to reach 0 ; count the total number of stesp ; iterate till we reach 0 ; count the digits in last ; decrease it by 1 ; find the number on whose division , we get the first digit ; first digit in last ; find the first number less than last where the first digit changes ; find the number of numbers with same first digit that are jumped ; count the steps ; the next number with a different first digit ; Driver code
< ? php function countdig ( $ m ) { if ( $ m == 0 ) return 0 ; else return 1 + countdig ( ( int ) ( $ m / 10 ) ) ; } function countSteps ( $ x ) { $ c = 0 ; $ last = $ x ; while ( $ last ) { $ digits = countdig ( $ last ) ; $ digits -= 1 ; $ divisor = pow ( 10 , $ digits ) ; $ first = ( int ) ( $ last / $ divisor ) ; $ lastnumber = $ first * $ divisor ; $ skipped = ( $ last - $ lastnumber ) / $ first ; $ skipped += 1 ; $ c += $ skipped ; $ last = $ last - ( $ first * $ skipped ) ; } return $ c ; } $ n = 14 ; echo countSteps ( $ n ) ;