Write an algorithm to convert the number 3998 into Roman numerals. Provide the Roman representation of the input number. Also, write a code for the algorithm. What about the numbers 5999 and 8999? Please provide the Roman representation of these numbers as well.