-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_roman_numerals.rb
50 lines (44 loc) · 1.28 KB
/
old_roman_numerals.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def find_divisor (dividend, divisor)
quotient = dividend / divisor
return quotient
end
def find_remainder (dividend, quotient, divisor)
remainder = dividend % (quotient * divisor)
return remainder
end
def get_numeral (decimal_places, quotient)
roman_symbols = {0 => ['I', 'V'], 1 => ['X', 'L'], 2 => ['C', 'D'], 3 => ['M']}
if quotient < 5
return roman_symbols[decimal_places][0] * quotient
else
return roman_symbols[decimal_places][1] + (roman_symbols[decimal_places][0] * (quotient - 5))
end
end
# ask for the number
while true
puts ''
puts 'Please type a whole number between 1 and 3000.'
puts '(To exit the program, hit enter on an empty line)'
typed_number = gets.chomp
decimal_places = typed_number.length
dividend = typed_number.to_i
roman_numeral = ''
if decimal_places == 0
break
end
if dividend.between?(1, 3000)
decimal_places.times do
decimal_places = decimal_places - 1
divisor = 10 ** decimal_places
if dividend > 0
quotient = find_divisor dividend, divisor
partial_numeral = get_numeral decimal_places, quotient
roman_numeral = roman_numeral + partial_numeral
dividend = find_remainder dividend, quotient, divisor
else
break
end
end
puts roman_numeral
end
end