**Xi'an's Og » R**, and kindly contributed to R-bloggers)

**S**ince the puzzle in this week (-end) edition of ** Le Monde** is not (easily) solvable via an R program, I chose to go back to an older puzzle that my students can solve.

Eleven [distinguishable] token are [arbitrarily] distributed around a 200 meter perimeter-long ring. They all start moving at the same speed, 18km/h, in arbitrary directions. When two tokens hit one another they instantaneously reverse directions while keeping the same absolute speed. How long does it take for all tokens to be back in their initial positions?

**T**he logical [non-computational] solution was given in an earlier edition: If all tokens move in the same direction, it takes 60×60/(18×5)=40 seconds to complete a turn and hence to see all tokens back to their initial positions. If they are moving into different directions, hits have no impact on the distribution of the tokens when they are not distinguished. Therefore, after 40 seconds, the repartition of the tokens is the same as at time 0, except for a permutation of the individual tokens. Given that there are 11 tokens, it takes at most 11 permutations to see all tokens back to their initial positions, i.e. 440 seconds. Here is the basic R resolution with a dynamic rendering of the moving tokens. It obviously gives 440 seconds as its output.

tolor=colorRampPalette(c("bisque1","sienna4"))(11) tokens=start=sort(sample(0:199,11)) # warning: plotting takes much more time plot(sin(2*pi*tokens/200),cos(2*pi*tokens/200),col=tolor, axes=F,xlab="",ylab="") speeds=5*sample(c(-1,1),11,rep=T) time=newtime=1/10 tokens=(tokens+speeds*newtime)%%200 for (i in (1:10)) for (j in ((i+1):11)) if (tokens[i]==tokens[j]) speeds[c(i,j)]=-speeds[c(i,j)] while (sum(abs(tokens-start))>0){ points(sin(2*pi*tokens/200),cos(2*pi*tokens/200), col="white",pch=19,axes=F,xlab="",ylab="") tokens=(tokens+speeds*newtime)%%200 time=time+newtime for (i in (1:10)) for (j in ((i+1):11)) if (tokens[i]==tokens[j]) speeds[c(i,j)]=-speeds[c(i,j)] points(sin(2*pi*tokens/200),cos(2*pi*tokens/200),col=tolor, pch=19) points(sin(2*pi*start/200),cos(2*pi*start/200),col=tolor) } time

Filed under: R, Statistics, University life Tagged: Le Monde, mathematical puzzle, R, R code

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

**Xi'an's Og » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...