#!/usr/bin/env python
# coding=utf-8
def int2roman(num):
ret=""
while num > 0 :
if num >= 1000:
ret += "M" * (num /1000)
num = num % 1000
elif num >= 100:
n3 = num/100
if n3 == 9:
ret+="CM"
elif n3>= 5:
ret += "D"
n3 -= 5
ret += n3 * "C"
elif n3 == 4:
ret += "CD"
else:
ret += n3 * "C"
num = num % 100
elif num >= 10:
n2 = num /10
if n2 == 9:
ret += "XC"
elif n2 >=5:
ret += "L"
n2 -=5
ret += n2 * "X"
elif n2 == 4:
ret += "XL"
else:
ret += n2 * "X"
num = num %10
elif num > 0:
if num == 9:
ret += "IX"
elif num >=5:
ret += "V"
num -= 5
ret += num * "I"
elif num == 4:
ret += "IV"
else:
ret += num * "I"
num = 0
#print(ret)
return ret
int2roman(18)
int2roman(19)
int2roman(1666)
int2roman(1888)
int2roman(1984)
int2roman(2000)
int2roman(3999)
Integer to Roman
Jul 17, 2016