<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/the-riddler-santa-needs-some-help-with-math/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rethinking.rbind.io/2018/12/22/the-riddler-santa-needs-some-help-with-math/"> R on R(e)Thinking</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>For the last <a rel="nofollow" href="https://fivethirtyeight.com/features/santa-needs-some-help-with-math/" target="_blank">Riddler</a> of the year I attempt to solve both the Express and Classic Riddlers!</p>
<h3>Riddler Express: How Long Will it Take Santa to Place his Reindeer?</h3>
<p>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.</p>
<p>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:<br>
<img alt="" src="https://i0.wp.com/rethinking.rbind.io/post/2018-12-22-the-riddler-santa-needs-some-help-with-math_files/ridlerexpressanta.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>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:</p>
<p>$$<br>
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 ) + …<br>
$$</p>
<p>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:</p>
<p>$$<br>
E[T]=\sum<em>{i=1}^{8}E[T</em>{i}]<br>
$$</p>
<p>To generalize this problem to a sled with N deer, I coded the expected time in R as follows:</p>
<pre><code>raindeer &lt;- function(N) {
  if (N==2) return(3/2)
  if (N==1) return(1) 
  n &lt;- N
  t &lt;- 2
  T &lt;- (1/n)
  while (n>2) {
    T &lt;- T + ( prod(seq(N-1,2)[1:(t-1)]) / prod(seq(N,3)[1:(t-1)]) ) * ((1/(n-1))*t)
    t &lt;- t+1
    n &lt;- n-1
  }
  T &lt;- T + ( prod(seq(N-1,2)[1:(t-2)]) / prod(seq(N,3)[1:(t-2)]) ) * ((1/(n))*t)
  T
}

sled &lt;- function(N) sum(sapply(1:N, raindeer))

sled(8)
</code></pre>
<p>[1] 22</p>
<p>So it will take Santa about 22 minutes to get his sled ready to go.</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="https://rethinking.rbind.io/2018/12/22/the-riddler-santa-needs-some-help-with-math/"> R on R(e)Thinking</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/W11ry1i_DpA" height="1"></a></div>