## Roman Numerals |

29 Aug 2014, 01:18
Eddie Muñoz (2 posts) |
I’ve been going about this book with the process of translating the written code into spoken word. That way it’s easier to see something starts and ends. However, now that I’m at the roman numerals exercises, I can’t for the life of me understand how to read, interpret, and create my own. Specifically this code. Please pardon the lack of formatting. I’ve tried to correct it but it just goes back to this manner of display. roman = ‘’ roman = roman + ‘M’ * (num / 1000) roman = roman + ‘D’ * (num % 1000 / 500) roman = roman + ‘C’ * (num % 500 / 100) roman = roman + ‘L’ * (num % 100 / 50) roman = roman + ‘X’ * (num % 50 / 10) roman = roman + ‘V’ * (num % 10 / 5) roman = roman + ‘I’ * (num % 5 / 1) roman Can anyone help me break it down please? |

07 Sep 2014, 04:25
Chris Pine (62 posts) |
First, sorry about the formatting issues. Use Second, I hope you are writing your own solutions rather than just focusing on mine! No amount of reading code can make up for a lack of writing code. :-) So, to answer your question:
The entire method is building up a single string, stored in With me so far? I just want to make sure you get the large-scale feel of what this method does before delving into the specifics. OK. For each of the middle lines, we add all of the appropriate letter. You can see that the first line is for adding all of the So far so good? All that’s left is to understand why the expressions in parentheses give us the numbers that we want. Remember that Let’s work through a few examples. Let’s first try `3 / 1000` ->`0` , so no M’s.`3 % 1000 / 500` ->`3 / 500` ->`0` , so no D’s.`3 % 500 / 100` ->`3 / 100` ->`0` , so no C’s.`3 % 100 / 50` ->`3 / 50` ->`0` , so no L’s.`3 % 50 / 10` ->`3 / 10` ->`0` , so no X’s.`3 % 10 / 5` ->`3 / 5` ->`0` , so no V’s.`3 % 5 / 1` ->`3 / 1` ->`3` , so we finally get three I’s!
So you see how the small value of 3 passed all the way down to the bottom line. Let’s try again, with `2000 / 1000` ->`2` , so we start with two M’s.`2000 % 1000 / 500` ->`0 / 500` ->`0` , so no D’s.`2000 % 500 / 100` ->`0 / 100` ->`0` , so no C’s.`2000 % 100 / 50` ->`0 / 50` ->`0` , so no L’s.`2000 % 50 / 10` ->`0 / 10` ->`0` , so no X’s.`2000 % 10 / 5` ->`0 / 5` ->`0` , so no V’s.`2000 % 5 / 1` ->`0 / 1` ->`0` , so no I’s.
So the M’s were pulled off right at the beginning. Finally, let’s look at `2014 / 1000` ->`2` , so we start with two M’s.`2014 % 1000 / 500` ->`14 / 500` ->`0` , so no D’s.`2014 % 500 / 100` ->`14 / 100` ->`0` , so no C’s.`2014 % 100 / 50` ->`14 / 50` ->`0` , so no L’s.`2014 % 50 / 10` ->`14 / 10` ->`1` , so one X.`2014 % 10 / 5` ->`4 / 5` ->`0` , so no V’s.`2014 % 5 / 1` ->`4 / 1` ->`4` , so four I’s.
Chris |

06 Sep 2014, 07:22
Eddie Muñoz (2 posts) |
Chris, Thank you so much for your reply. And sorry for my delay. So you said / stands for multiplication? I’m Thinking you meant to say division? I hate to say it but I’m still a bit confused. So for your example on 2014. I’m reading on the first line where we divide 2014/ 1000 roman = 2014 + m * (2014/1000) Is that right? or is this where I’m interpreting and going wrong? Again, thank you for your reply, Eddie M. |

07 Sep 2014, 04:30
Chris Pine (62 posts) |
Yes, I meant division. :) Fixed now. For the first line, remember that
In other words, we set Does that help? |

You must be logged in to comment |