Playing with Roman numerals

[This article was first published on r.iresmi.net, 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.

What is the longest year number (yet) written in Roman numerals ?

library(tidyverse)

tibble(y = 1:2020) %>%
  mutate(r = as.roman(y),
         l = str_length(r)) %>%
  slice_max(l)
# A tibble: 1 x 3
      y r                 l
  <int> <roman>       <int>
1  1888 MDCCCLXXXVIII    13

It is year 1888, with 13 characters…

And the largest writable number being 3899, according to the strict rules in R (however some say it’s 3999),

tibble(y = 1:5000) %>%
	mutate(r = as.roman(y),
	       l = str_length(r)) %>%
	filter(lead(is.na(l))) %>% 
  	slice_min(l)
# A tibble: 1 x 3
      y r               l
  <int> <roman>     <int>
1  3899 MMMDCCCXCIX    11

the longest overall year will be year 3888 with 15 characters.

tibble(y = 1:3899) %>%
	mutate(r = as.roman(y),
	       l = str_length(r)) %>%
	slice_max(l)
# A tibble: 1 x 3
      y r                   l
  <int> <roman>         <int>
1  3888 MMMDCCCLXXXVIII    15

Nice pattern :

tibble(y = 1:3899) %>%
	mutate(r = as.roman(y),
	       l = str_length(r)) %>%
	ggplot(aes(y, l)) +
	# geom_col(width = 1) +
	geom_point(alpha = .2) +
	# geom_line(alpha = .5) +
	geom_smooth() +
	labs(title = "Characters length of roman numeral years",
		 x = "year",
		 y = "characters")
Characters length of roman numeral years

And there are only eleven palindromic years :

tibble(y = 1:3899) %>%
	mutate(r = as.character(as.roman(y)),
	       rr = stringi::stri_reverse(r)) %>% 
	filter(r == rr,
	       str_length(r) > 1)
# A tibble: 11 x 3
       y r     rr   
   <int> <chr> <chr>
 1     2 II    II   
 2     3 III   III  
 3    19 XIX   XIX  
 4    20 XX    XX   
 5    30 XXX   XXX  
 6   190 CXC   CXC  
 7   200 CC    CC   
 8   300 CCC   CCC  
 9  1900 MCM   MCM  
10  2000 MM    MM   
11  3000 MMM   MMM  

To leave a comment for the author, please follow the link and comment on their blog: r.iresmi.net.

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)