**Tony's bubble universe » R**, and kindly contributed to R-bloggers)

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 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...