**R on R(e)Thinking**, and kindly contributed to R-bloggers)

For the last Riddler of the year I attempt to solve both the Express and Classic Riddlers!

### Riddler Express: How Long Will it Take Santa to Place his Reindeer?

We need to find out how long it will take Santa to place his reindeer in the correct sled positions, if he proceeds at random. It takes a minute to harness a raindeer, and the raindeer will grunt to indicate it’s in the right position.

Thinking in terms of a probabiity tree, if we consider the first position in the sled, the expected time that it will take to place the raindeer correctly looks like this:

The expected time that it will take for the first raindeer placed correctly is the sum of all the paths leading to the green circles. For example:

$$

E[T_{8}] = \left (\frac{1}{8} \cdot 1 \right) + \left (\frac{7}{8} \cdot \frac{1}{7} \cdot 2 \right ) + \left (\frac{7}{8} \cdot \frac{6}{7} \cdot\frac{1}{6} \cdot 3 \right ) + …

$$

This process can be repeated for each of the eight deer, with the tree getting one branch shorter for each deer that has been correctly placed. The expected time to place all eight deer when then be the sum of the expected time for each deer:

$$

E[T]=\sum*{i=1}^{8}E[T*{i}]

$$

To generalize this problem to a sled with N deer, I coded the expected time in R as follows:

```
raindeer <- function(N) {
if (N==2) return(3/2)
if (N==1) return(1)
n <- N
t <- 2
T <- (1/n)
while (n>2) {
T <- T + ( prod(seq(N-1,2)[1:(t-1)]) / prod(seq(N,3)[1:(t-1)]) ) * ((1/(n-1))*t)
t <- t+1
n <- n-1
}
T <- T + ( prod(seq(N-1,2)[1:(t-2)]) / prod(seq(N,3)[1:(t-2)]) ) * ((1/(n))*t)
T
}
sled <- function(N) sum(sapply(1:N, raindeer))
sled(8)
```

[1] 22

So it will take Santa about 22 minutes to get his sled ready to go.

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

**R on R(e)Thinking**.

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