Let’s Go to Rome

[This article was first published on Frank Portman, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Although virtually obsolete, Roman Numerals are subtly embedded into our culture. From the Super Bowl and Olympics to royal titles, Roman Numerals refuse to fully be extinguished from our every day lives. And that’s not without reason. All numbers are beautiful and Roman Numerals are no exception, even if they are written a little differently from their Arabic counterparts.

In this post, we’ll examine some fascinating properties of Roman Numerals – namely the lengths of Roman Numerals in succession.

First, we define a simple Arabic –> Roman Numeral converter. Start by creating two vectors, one for the 13 Roman symbols and another for the Arabic counterparts. Next, a simple for/while combination iterates through the arrays and chooses the appropriate Roman symbols while iteratively decreasing the input variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
<span class="line">arabic <span class="o"><-</span> <span class="kt">c</span><span class="p">(</span><span class="m">1000</span><span class="p">,</span> <span class="m">900</span><span class="p">,</span> <span class="m">500</span><span class="p">,</span> <span class="m">400</span><span class="p">,</span> <span class="m">100</span><span class="p">,</span> <span class="m">90</span><span class="p">,</span> <span class="m">50</span><span class="p">,</span> <span class="m">40</span><span class="p">,</span> <span class="m">10</span><span class="p">,</span> <span class="m">9</span><span class="p">,</span> <span class="m">5</span><span class="p">,</span> <span class="m">4</span><span class="p">,</span> <span class="m">1</span><span class="p">)</span>
</span><span class="line">roman <span class="o"><-</span> <span class="kt">c</span><span class="p">(</span><span class="s">"M"</span><span class="p">,</span> <span class="s">"CM"</span><span class="p">,</span> <span class="s">"D"</span><span class="p">,</span> <span class="s">"CD"</span><span class="p">,</span> <span class="s">"C"</span><span class="p">,</span> <span class="s">"XC"</span><span class="p">,</span> <span class="s">"L"</span><span class="p">,</span> <span class="s">"XL"</span><span class="p">,</span> <span class="s">"X"</span><span class="p">,</span> <span class="s">"IX"</span><span class="p">,</span> <span class="s">"V"</span><span class="p">,</span> <span class="s">"IV"</span><span class="p">,</span> <span class="s">"I"</span><span class="p">)</span>
</span><span class="line">
</span><span class="line">toRoman <span class="o"><-</span> <span class="kr">function</span><span class="p">(</span>n<span class="p">)</span> <span class="p">{</span>
</span><span class="line">  ans <span class="o"><-</span> <span class="kt">c</span><span class="p">()</span>
</span><span class="line">  <span class="kr">for</span><span class="p">(</span>i <span class="kr">in</span> <span class="m">1</span><span class="o">:</span><span class="kp">length</span><span class="p">(</span>arabic<span class="p">))</span> <span class="p">{</span>
</span><span class="line">    <span class="kr">while</span><span class="p">(</span>n <span class="o">>=</span> arabic<span class="p">[</span>i<span class="p">])</span> <span class="p">{</span>
</span><span class="line">      ans <span class="o"><-</span> <span class="kt">c</span><span class="p">(</span>ans<span class="p">,</span> roman<span class="p">[</span>i<span class="p">])</span>
</span><span class="line">      n <span class="o"><-</span> n <span class="o">-</span> arabic<span class="p">[</span>i<span class="p">]</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line">  ans
</span><span class="line"><span class="p">}</span>
</span>

Next we want to find the lengths of each of these representations:

1
2
3
4
5
6
<span class="line">cands <span class="o"><-</span> <span class="kt">list</span><span class="p">()</span>
</span><span class="line">cands<span class="p">[</span><span class="m">1</span><span class="o">:</span><span class="m">3999</span><span class="p">]</span> <span class="o"><-</span> <span class="m">1</span><span class="o">:</span><span class="m">3999</span>
</span><span class="line">cands <span class="o"><-</span> <span class="kp">lapply</span><span class="p">(</span>cands<span class="p">,</span> toRoman<span class="p">)</span>
</span><span class="line">cands <span class="o"><-</span> <span class="kp">lapply</span><span class="p">(</span>cands<span class="p">,</span> <span class="kr">function</span><span class="p">(</span>x<span class="p">)</span> <span class="kp">unlist</span><span class="p">(</span><span class="kp">strsplit</span><span class="p">(</span>x<span class="p">,</span> <span class="s">""</span><span class="p">)))</span>
</span><span class="line">lens <span class="o"><-</span> <span class="kp">lapply</span><span class="p">(</span>cands<span class="p">,</span> <span class="kp">length</span><span class="p">)</span>
</span><span class="line">lens <span class="o"><-</span> <span class="kp">as.numeric</span><span class="p">(</span>lens<span class="p">)</span>
</span>

Finally, plot the result:

1
2
3
4
<span class="line">qplot<span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="m">3999</span><span class="p">,</span> lens<span class="p">,</span> geom <span class="o">=</span> <span class="s">"line"</span><span class="p">)</span> <span class="o">+</span> xlab<span class="p">(</span><span class="s">"Numbers"</span><span class="p">)</span> <span class="o">+</span>
</span><span class="line">  ylab<span class="p">(</span><span class="s">"Length of Roman Numeral"</span><span class="p">)</span> <span class="o">+</span>
</span><span class="line">  ggtitle<span class="p">(</span><span class="s">"1 - 3999 Length of Roman Numerals"</span><span class="p">)</span> <span class="o">+</span>
</span><span class="line">  geom_smooth<span class="p">(</span>method <span class="o">=</span> <span class="s">"lm"</span><span class="p">)</span>
</span>

What a fascinating relationship! The lengths seem to take a sinusoidal shape with positive drift. We can see that although the average length keeps rising, the plot goes through very regular cycles of rising and falling.

To leave a comment for the author, please follow the link and comment on their blog: Frank Portman.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)