The Riddler: Santa Needs Some Help With Math

[This article was first published on R on R(e)Thinking, 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.

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.

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

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)