# Project Euler — problem 19

**Tony's bubble universe » R**, 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.

I’ve been working overtime last weekend. Although I suffered little from the Monday syndrome, I still need a break. So, I’m back to the Project Euler after days of Olympic data digging. Today, I’m gonna to solve the 19th problem.

You are given the following information, but you may prefer to do some research for yourself. 1) 1 Jan 1900 was a Monday. 2) Thirty days has September, April, June and November. All the rest have thirty-one, Saving February alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine. 3) A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

This is another require-your-patience problem, at least to me. Without using built-in functions in R, one need to count leap years correctly. Once counting is right, the problem is a piece of cake.

^{?}View Code RSPLUS

1 2 3 4 5 6 7 |
date.leap <- c(1:31, 1:29, 1:31, 1:30, 1:31, 1:30, 1:31, 1:31, 1:30, 1:31, 1:30, 1:31) date.norm <- c(1:31, 1:28, 1:31, 1:30, 1:31, 1:30, 1:31, 1:31, 1:30, 1:31, 1:30, 1:31) dates <- c(date.norm, rep(c(rep(date.norm, 3), date.leap), 25)) # list all dates from 1900 to 2000 firsts <- which(dates == 1) # get those 1st dates firsts <- firsts[-c(1:12)] # the first 12 is from 1900, thus are droped result <- sum(firsts %% 7 == 0) cat("The result is:", result, "\n") |

Two additional notes:

- By using build-in functions weekdays(), it’s easy to tell the day for any give day.
- Or you could just guess the result with (25*366+75*365)/7. It won’t take too many guesses 🙂

**leave a comment**for the author, please follow the link and comment on their blog:

**Tony's bubble universe » R**.

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.