<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/euler-problem-19-counting-sundays-when-does-the-week-start/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r.prevos.net/euler-problem-19/"> The Devil is in the Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Euler Problem 19 is so trivial it is almost not worth writing an article about. One interesting aspect of this problem is the naming of weekdays and deciding which day the week starts. This issue is more complex than it sounds because data science is in essence not about data but about people.</p>
<p><iframe width="450" allowfullscreen="true" src="https://www.youtube.com/embed/JEyuQd-zMeg?version=3&rel=1&%23038;fs=1&%23038;autohide=2&%23038;showsearch=0&%23038;showinfo=1&%23038;iv_load_policy=1&%23038;wmode=transparent" type="text/html"></iframe></p>
<h2>Euler Problem 19 Definition</h2>
<ul>
<li>1 Jan 1900 was a Monday.</li>
<li>Thirty days has September, April, June and November.</li>
<li>All the rest have thirty-one,</li>
<li>Saving February alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine.</li>
<li>A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.</li>
</ul>
<p><a target="_blank" rel="nofollow" href="https://projecteuler.net/problem=19">How many Sundays fell on the first of the month during the twentieth century</a> (1 Jan 1901 to 31 Dec 2000)?</p>
<h2>Solution</h2>
<p>The problem can be quickly solved with R base code and a tiny bit faster when using the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/lubridate/">lubridate package</a>.</p>
<pre>
# Base R-code
dates &lt;- seq.Date(as.Date(&quot;1901/01/01&quot;), as.Date(&quot;2000/12/31&quot;), &quot;days&quot;)
days &lt;- rep(1:7, length.out = length(dates))
answer &lt;- sum(days[substr(dates, 9, 10) == &quot;01&quot;] == 1)
print(answer)

#Using Lubridate
library(lubridate, quietly = TRUE)
answer &lt;- sum(wday(dates[substr(dates, 9, 10) == &quot;01&quot;]) == 1)
print(answer) 	 
</pre>
<p>To draw out this post a little bit further I wrote some code to solve the problem without using the calendar functions in R.</p>
<pre>
week.day &lt;- 0
answer &lt;- 0
for (y in 1901:2000) {
    for (m in 1:12) {
        max.day &lt;- 31
        if (m %in% c(4, 6, 9, 11)) max.day &lt;- 30
        # Leap years
        if (m == 2) {
            if (y %% 4 == 0 &amp; y %% 100 != 0 | y %% 400 == 0) max.day &lt;- 29
            else max.day &lt;- 28
            }
        for (d in 1:max.day) {
            week.day &lt;- week.day + 1
            if (week.day == 8) week.day &lt;- 1
            if (week.day == 1 &amp; d == 1) answer &lt;- answer + 1
        }
    }
}
print(answer)
</pre>
<h2>Which day does the week start?</h2>
<p>The only aspect remotely interesting about this problem is the conversion from weekdays to numbers. In R, Monday is considered day one, which makes sense in the Christian context of Western culture. Saturday and Sunday are the weekend, the two last days of the week so they are day 6 and 7. According to international standard <a target="_blank" rel="nofollow" href="http://www.iso.org/iso/home/standards/iso8601.htm">ISO 8601</a>, Monday is the first day of the week. Although this is the international standard, several countries, including the United States and Canada, consider Sunday to be the first day of the week.</p>
<p>The international standard is biased towards Christianity. The Christian or Western world marks Sunday as their day of rest and worship. Muslims refer to Friday as their day of rest and prayer. The Jewish calendar counts Saturday—the Sabbath—as the day of rest and worship. This idea is also shared by Seventh-Day Adventists.</p>
<p>this example shows that data science is not only about data: it is always about people and how they interpret the world.</p>
<p><iframe width="450" src="http://chartsbin.com/embed/41671"> </iframe></p>
<p>via <a title="First Day of the Week in Different Countries" target="_blank" rel="nofollow" href="http://chartsbin.com/view/41671">chartsbin.com</a></p>
<p>The post <a target="_blank" rel="nofollow" href="http://r.prevos.net/euler-problem-19/">Euler Problem 19: Counting Sundays — When does the week start?</a> appeared first on <a target="_blank" rel="nofollow" href="http://r.prevos.net/">The Devil is in the Data</a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r.prevos.net/euler-problem-19/"> The Devil is in the Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/T3_XeYZJRl0"></a></div>