EUROPEAN ITERATION
We know about number systems: The Roman numerals and the alternative place-value system with a given base.
For the purposes of this problem, we limit ourselves to
- Roman numerals with values up to 3999
MMMCMXCIX
- "Place value system" numbers having bases from 2 (with possible symbols 0, 1) through 36 (with possible symbols 0, 1, ..., 9, A, ... ,Z)
Consider the following procedure:
- Accept a natural number
N
(in base 10). - If N lies in the closed interval [1,3999], i.e. between 1 and 3999 (both inclusive), convert
N
toR
, its Roman numeral representation; else outputN
as the result and stop. - Identify the base in which the value of R, now considered to be in "place value system", is least and calculate its value in base 10, replacing N with this value.
- Repeat from step 2.
Constraints
1 <= N <= 3999
Input Format
- A single Integer
N
.
Output
- Converted
N
Test Case
Example Input
1
Output
45338950
Explanation
The procedure goes as follows in this case:
- Accept N = 1.
- Since 1 lies in
[1,3999]
, covert it to RomanR = I
- The least value of
I
(in bases19
and above) is18
in base 10. HenceN = 18
. - Repeating step 2, since
18
lies in[1,3999]
, convert it toR = XVIII
- The least value of
XVIII
(in base34
) is33*34^4+31*34^3+18*34^2+18*34+18 or N = 45338950
- Repeating step 2, since
45338950
lies outside[1,3999]
output45338950
and stop.
Here's how the conversions go:
Input = 1 => I => 18 => XVIII => 45338950 = Output
Solution:
def roman(n): #gets the roman number
r=''
num=[1,4,5,9,10,40,50,90,100,400,500,900,1000]
sym=['I','IV','V','IX','X','XL','L','XC','C','CD','D','CM','M']
i=12
while(n):
d=int(n/num[i])
n=n%num[i]
while(d):
r=r+sym[i]
d=d-1
i=i-1
return(r)
def dec(rom,base): #calculates the decimal value of the roman numeral with given base
return(int(rom,base))
n=int(input())
ch='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
while(n>=1 and n<=3999):
p=roman(n)
b=ch.find(max(p))+1 #finds the base of the roman numeral
n=dec(p,b)
print(n)
Comments
Post a Comment