21

2

**Goal:**

Write a function that takes a number as input and returns a short-hand roman numeral for that number as output.

**Roman Numeral Symbols:**

```
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1,000
```

For an example of what I mean when I say "short-hand roman numerals", let's consider finding a roman numeral to represent 1983, because that is the year I was born. One option is to do this the normal way (10 letters):

**1983** = **MCMLXXXIII** = (1000 - 100 + 1000 + 50 + 30 + 3)

The other option is to do it the short-hand way (6 characters):

**1983** = **MXVIIM** = (1000 - (10 + 10) + 1000 + 3)

Do you know what this means?!?!!?? If I was roman I could have saved 4 characters every single time I wrote my birth date! Woot Woot!!

However, before I get ahead of myself in excitement, I have a question to write, so I should probably define the short-hand roman numeral rules so we are all on the same page:

**Short-Hand Roman Numeral Rules:**

- Always consider symbols from left to right until there are no more characters to consider.
- If there are no higher-valued symbols to the right of the current symbol:
- Add the value of the current symbol to the running total of this roman numeral.

- If there are higher-valued symbols to the right of the symbol you are considering:
- Locate the rightmost highest-valued symbol to the right of the current symbol
- Consider all the characters up to that symbol as one roman numeral
- Calculate the value of that roman numeral using these steps
- Subtract the value of that roman numeral from the running total of this roman numeral.
- Move to the next symbol after the group you just considered

- Each roman numeral must have at least 1 symbol in it.
- That's it! Anything following these rules will be accepted!

**Examples:**

```
IIIIV = (-(1+1+1+1)+5) = 1 //Don't ask me why you'd want to do this!
VVX = (-(5+5) + 10) = 0 //Who said you couldn't represent 0 with roman numerals?!!?
VVXM = (-(-(5+5) + 10) + 1000) = 1000 //Again...don't ask me why you'd want to do this!
MXIIXMI = (1000-(10-(1+1)+10)+1000+1) = 1983 //Ahhh...such a great year :)
```

**Question Rules:**

Make a function that takes a single number as input and returns a roman numeral for that number as output using the above rules. Calculate the

**codeGolfScore**of this function.`example input: 2011 example possible output: MMXI another possible output: MMVVIVV //(2000 + 10 - 4 + 5)`

Using your function from rule 1, generate the roman numerals between -1000 (that's right, NEGATIVE one-thousand) and 3000. Then sum up the character length of these roman numerals to get your

**totalCharacterCount**. Here's some pseudocode to clarify:`totalCharacterCount = 0; for(currentNumber = -1000; currentNumber <= 3000; currentNumber++){ totalCharacterCount += getRomanNumeral(currentNumber).length; } return totalCharacterCount;`

**finalScore**=**codeGolfScore**+**totalCharacterCount**- Lowest
**finalScore**wins!

Note: As the totalCharacter count will be in the ten-thousands+, the character-length algorithm should be top priority. Code-golf scores are just the tie-breaker in case multiple users find the optimal algorithm or algorithms that are close to each other.

Good luck, and have fun at your MMXII celebrations tomorrow night!!!

Is IDC as well as DIC allowed for 599 (499+100 vs. 500+99)? – user unknown – 2012-04-19T18:02:22.453

@userunknown: Yep, both are allowed. – Briguy37 – 2012-04-19T18:16:36.740

1Great task! However, could you give an example of how a negative roman shorthand looks like? Does

`DDDDM`

stand for`-1000`

? – pimvdb – 2011-12-30T19:00:38.253@pimvdb You got it! – Briguy37 – 2011-12-30T19:37:30.687

A question regarding the special case zero: Is

`""`

allowed for zero or do we have to use`VVX`

or something equivalent? – Howard – 2011-12-30T19:46:22.633@Howard: Great question, I hadn't thought of that! I've added roman numeral rule 4 for which clarifies that case. – Briguy37 – 2011-12-30T20:00:59.650

1"Locate the rightmost highest-valued symbol to the right of the current symbol" -- which wins, rightmost or highest-valued? i.e., is

`IXV = -(-1 + 10) + 5 = -4`

(rightmost wins), or`IXV = -1 + 10 + 5 = 14`

(highest-valued wins)? – Keith Randall – 2011-12-30T21:06:21.970@KeithRandall I think it must be latter. Otherwise it would have to read "rightmost

higher-valued". – Howard – 2011-12-30T21:20:33.663@KeithRandall: Highest value wins, so

`IXV = 14`

, and`IXVX =-14+10= -4`

. – Briguy37 – 2011-12-30T21:32:49.663