On Feb 2, 2008 1:33 PM, Kelly Tanguay <kelly.tanguay / cox.net> wrote:
> This is my take on the same problem:
>
>
> def roman_num number
> set1 = [ 1, 5, 10, 50, 100, 500, 1000 ]
> set2 = [ 'I', 'V', 'X', 'L', 'C', 'D', 'M' ]
> numeral = []
>   while number > 0
>     if (number/(set1.last)) >= 1
>       roman = (number/(set1.last))
>       numeral.push((set2.pop)*roman)
>       number = (number%(set1.pop))
>     else
>       set2.pop
>       set1.pop
>     end
>   end
>   puts 'Old Roman Numeral is ' + numeral.join + '.'
> end
>
> puts 'Please enter a number to see what it is in old roman numerals.'
> number = gets.chomp.to_i
> while number < 1 || number > 3999
>   puts 'Please enter a number between 1 and 3999'
>   number = gets.chomp.to_i
> end
> roman_num number

Just for fun. Certainly not great for speed for large numbers, but the
integer max was low so...

H = Hash[*(([1,5,10,50,100,500,1000].zip %w|I V X L C D M|).flatten)]
def roman(n, s="")
  H.keys.sort.reverse.each do |k|
    s << (H[k] * (n / k))
    n %= k
  end
  s
end
puts roman(ARGV[0].to_i)

The usage would simply be "ruby <filename>.rb <number>"

I'm sure someone could come up with a one-liner, though.

Todd