Transform N to Minimum possible value | Let initialising the answer ; Function to find the digitsum ; Iterate over all digits and add them ; Return the digit su , ; Function to transform N to the minimum value ; If the final value is lesser than least value ; If final value is equal to least value then check for lesser number of steps to reach this value ; The value will be obtained in less than 15 steps as proved so applying normal recursive operations ; Driver Code ; Function call ; Prlet the answers
let min_val = Number . MAX_VALUE ; let min_steps = 0 ; function sumOfDigits ( n ) { let s = n . toString ( ) ; let sum = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { sum += ( s [ i ] - ' ' ) ; } return sum ; } function Transform ( n , d , steps ) { if ( n < min_val ) { min_val = n ; min_steps = steps ; } else if ( n == min_val ) { min_steps = Math . min ( min_steps , steps ) ; } if ( steps < 15 ) { Transform ( sumOfDigits ( n ) , d , steps + 1 ) ; Transform ( n + d , d , steps + 1 ) ; } } let N = 9 , D = 3 ; Transform ( N , D , 0 ) ; document . write ( min_val + " " + min_steps ) ;