<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/a-thread-to-bin-them-all-puzzle/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://xianblog.wordpress.com/2018/07/09/a-thread-to-bin-them-all-puzzle/"> R – Xi'an's Og</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img data-orig-file="https://xianblog.files.wordpress.com/2018/07/mcqmc1810.jpg" data-orig-size="1440,1920" data-medium-file="https://xianblog.files.wordpress.com/2018/07/mcqmc1810.jpg?w=225" data-attachment-id="36523" data-image-meta="{" data-image-description="" src="https://xianblog.files.wordpress.com/2018/07/mcqmc1810.jpg?w=120&h=160&fit=120%2C160&resize=120%2C160" data-permalink="https://xianblog.wordpress.com/2018/07/09/a-thread-to-bin-them-all-puzzle/olympus-digital-camera-475/" data-large-file="https://xianblog.files.wordpress.com/2018/07/mcqmc1810.jpg?w=450" alt="" data-image-title="" sizes="(max-width: 120px) 100vw, 120px" width="120" data-comments-opened="1" class="wp-image-36523" data-recalc-dims="1" height="160"> <img data-orig-file="https://xianblog.files.wordpress.com/2018/07/mcqmc1811.jpg" data-orig-size="1440,1920" data-medium-file="https://xianblog.files.wordpress.com/2018/07/mcqmc1811.jpg?w=225" data-attachment-id="36524" data-image-meta="{" data-image-description="" src="https://xianblog.files.wordpress.com/2018/07/mcqmc1811.jpg?w=120&h=161&fit=120%2C161&resize=120%2C161" data-permalink="https://xianblog.wordpress.com/2018/07/09/a-thread-to-bin-them-all-puzzle/olympus-digital-camera-476/" data-large-file="https://xianblog.files.wordpress.com/2018/07/mcqmc1811.jpg?w=450" alt="" data-image-title="OLYMPUS DIGITAL CAMERA" sizes="(max-width: 120px) 100vw, 120px" width="120" data-comments-opened="1" class="wp-image-36524" data-recalc-dims="1" height="161"> <img data-orig-file="https://xianblog.files.wordpress.com/2018/07/mcqmc1815.jpg" data-orig-size="1440,1920" data-medium-file="https://xianblog.files.wordpress.com/2018/07/mcqmc1815.jpg?w=225" data-attachment-id="36525" data-image-meta="{" data-image-description="" src="https://xianblog.files.wordpress.com/2018/07/mcqmc1815.jpg?w=120&h=160&fit=120%2C160&resize=120%2C160" data-permalink="https://xianblog.wordpress.com/2018/07/09/a-thread-to-bin-them-all-puzzle/olympus-digital-camera-477/" data-large-file="https://xianblog.files.wordpress.com/2018/07/mcqmc1815.jpg?w=450" alt="" data-image-title="OLYMPUS DIGITAL CAMERA" sizes="(max-width: 120px) 100vw, 120px" width="120" data-comments-opened="1" class="wp-image-36525" data-recalc-dims="1" height="160"></p>
<p><strong>T</strong>he most recent riddle on the Riddler consists in finding the shorter sequence of digits (in 0,1,..,9) such that all 10⁴ numbers between 0 (or 0000) and 9,999 can be found as a group of consecutive four digits. This sequence is obviously longer than 10⁴+3, but how long? On my trip to Brittany last weekend, I wrote an R code first constructing the sequence at random by picking with high preference the next digit among those producing a new four-digit number</p>
<pre>tenz=10^(0:3)
wn2dg=function(dz) 1+sum(dz*tenz)

seqz=rep(0,10^4)
snak=wndz=sample(0:9,4,rep=TRUE)
seqz[wn2dg(wndz)]=1
while (min(seqz)==0){
  wndz[1:3]=wndz[-1];wndz[4]=0
  wndz[4]=sample(0:9,1,prob=.01+.99*(seqz[wn2dg(wndz)+0:9]==0))
  snak=c(snak,wndz[4])
  sek=wn2dg(wndz)
  seqz[sek]=seqz[sek]+1}
</pre>
<p>which usually returns a value above 75,000. I then looked through the sequence to eliminate useless replicas</p>
<pre>for (i in sample(4:(length(snak)-5))){
 if ((seqz[wn2dg(snak[(i-3):i])]>1)
  &amp;(seqz[wn2dg(snak[(i-2):(i+1)])]>1)
  &amp;(seqz[wn2dg(snak[(i-1):(i+2)])]>1)
  &amp;(seqz[wn2dg(snak[i:(i+3)])]>1)){
   seqz[wn2dg(snak[(i-3):i])]=seqz[wn2dg(snak[(i-3):i])]-1
   seqz[wn2dg(snak[(i-2):(i+1)])]=seqz[wn2dg(snak[(i-2):(i+1)])]-1
   seqz[wn2dg(snak[(i-1):(i+2)])]=seqz[wn2dg(snak[(i-1):(i+2)])]-1
   seqz[wn2dg(snak[i:(i+3)])]=seqz[wn2dg(snak[i:(i+3)])]-1
   snak=snak[-i]
   seqz[wn2dg(snak[(i-3):i])]=seqz[wn2dg(snak[(i-3):i])]+1
   seqz[wn2dg(snak[(i-2):(i+1)])]=seqz[wn2dg(snak[(i-2):(i+1)])]+1
   seqz[wn2dg(snak[(i-1):(i+2)])]=seqz[wn2dg(snak[(i-1):(i+2)])]+1}}
</pre>
<p>until none is found. A first attempt produced 12,911 terms in the sequence. A second one 12,913. A third one 12,871. Rather consistent figures but not concentrated enough to believe in achieving a true minimum. An overnight run produced 12,779 as the lowest value.</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://xianblog.wordpress.com/2018/07/09/a-thread-to-bin-them-all-puzzle/"> R – Xi'an's Og</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/32154U6tUC8" height="1"></a></div>