# Le Monde puzzle [#743]

**Xi'an's Og » R**, 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.

**A**s ** Le Monde** weekend has yet again changed its format (with so much more advertisements for luxurious items that I sometimes wonder whether or not this is the weekend edition of

**!], it took me a while to locate the mathematical puzzle. The good news is there now is a science&techno leaflet with, at the end, the math puzzle!**

*Le Monde**(Sorry, this is of no relevance for anyone but me!)*

**T**he problem of last week is loosely connected with random walks on a grid. Given a nxm grid, consider all the shortest length paths linking (0,0) with (n,m). They all are of length n+m and can be represented as a sequence of *l*‘s and *d*‘s (for *left *and *down*). The question amounts to compute the probability that two of those paths intersect “in the middle”, i.e. after (n+m)/2 steps if (n+m) is even, or share the edge from the (n+m-1)/2 node to the (n+m+1)/2 node.

**W**hen (n+m) is even, if the meeting node is (i,j), with i+j=(n+m)/2, there are

moves from (0,0) to (i,j) and then again

moves from (i,j) to (n,m) (since the total number of down moves is n). The number of paths going from (0,0) to (n,m) through (i,j) is therefore

and the probability of meeting in (i,j) is

hence the probability of meeting somewhere is

whose numerator is equal to

according to Wolfram alpha when n>m…. And to

otherwise. Nothing we can compute in R, I am afraid. *(Feel free to process the case when n+m is odd as an exercise!)*

**N**ow, the original problem involved a random choice of direction (*left *and *down*) at each node (if any), meaning all paths do not have the same probability. So here is a (plain) R code aiming at approximating the probability:

N=10^3 n=7 m=11 genepath=function(n,m){ path=matrix(0,ncol=2,nrow=n+m+1) for (t in 2:(n+m+1)){ if (max(path[,1]-n,path[,2]-m)<0){ path[t,]=path[t-1,]+sample(c(0,1),2) }else{ path[t,]=path[t-1,]+c((path[t-1,1]

Filed under: R, Statistics Tagged: confluent hypergeometric function, RToleave a commentfor the author, please follow the link and comment on their blog:Xi'an's Og » R.

R-bloggers.com offersdaily e-mail updatesabout 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.