Minimum number with digits as 4 and 7 only and given sum | Prints minimum number with given digit sum and only allowed digits as 4 and 7. ; Cases where all remaining digits are 4 or 7 ( Remaining sum of digits should be multiple of 4 or 7 ) ; If both 4 s and 7 s are there in digit sum , we subtract a 4. ; Driver code
function findMin ( sum ) { var a = 0 , b = 0 ; while ( sum > 0 ) { if ( sum % 7 == 0 ) { b ++ ; sum -= 7 ; } else if ( sum % 4 == 0 ) { a ++ ; sum -= 4 ; } else { a ++ ; sum -= 4 ; } } if ( sum < 0 ) { document . write ( " " ) ; return ; } for ( i = 0 ; i < a ; i ++ ) document . write ( " " ) ; for ( i = 0 ; i < b ; i ++ ) document . write ( " " ) ; document . write ( ) ; } findMin ( 15 ) ;