(This article was first published on R – Xi'an's Og, and kindly contributed to R-bloggers)
A simple riddle express this week on The Riddler, about finding the years between 2001 and 2099 with the most cases when day x month = year [all entries with two digits]. For instance, this works for 1 January, 2001 since 01=01 x 01. The only difficulty in writing an R code for this question is to figure out the number of days in a given month of a given year (in order to include leap years).
The solution was however quickly found on Stack Overflow and the resulting code is
#safer beta quantilenumOD <- function(date) { m <- format(date, format="%m") while (format(date, format="%m") == m) date <- date + 1 return(as.integer(format(date - 1, format="%d")))}dayz=matrix(31,12,99)for (i in 2001:2099)for (j in 2:11) dayz[j,i-2000]=numOD(as.Date( paste(i,"-",j,"-1",sep=""),"%Y-%m-%d"))monz=rep(0,99)for (i in 1:99){for (j in 1:12) if ((i==(i%/%j)*j)&((i%/%j)<=dayz[j,i])) monz[i]=monz[i]+1}
The best year in this respect being 2024, with 7 occurrences of the year being the product of a month and a day…
var vglnk = { key: '949efb41171ac6ec1bf7f206d57e90b8' }; (function(d, t) {var s = d.createElement(t); s.type = 'text/javascript'; s.async = true;s.src = '//cdn.viglink.com/api/vglnk.js';var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r); }(document, 'script'));To leave a comment for the author, please follow the link and comment on their blog: R – Xi'an's Og.
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...