Archive for R

Riddle of the lanes

Posted in Books, Kids, R with tags , , , , , on July 13, 2020 by xi'an

An express riddle from the Riddler about reopening pools, where lanes are allowed provided there is no swimmer in the lane or in any of the adjacent lanes. If swimmers pick their lane at random (while they can), what is the average number of occupied lanes?

If there are n lanes and E(n) is the expected number of swimmers, E(n) satisfies a recurrence relation determined by the location of the first swimmer:

E(n)=1+\frac{1}{n}[2E(n-2)+\sum_{i=2}^{n-1}\{E(i-2)+E(n-i-1)\}]

with E(0)=0, E(1)=E(2)=1. The above can be checked with a quick R experiment:

en=0
for(t in 1:T){
   la=rep(u<-0,N)
   while(sum(la)<N){
     i=sample(rep((1:N)[!la],2),1)
     la[max(1,i-1):min(N,i+1)]=1
     u=u+1}
   en=en+u}

Le Monde puzzle [#1149]

Posted in Books, Kids, pictures, R with tags , , , , , , on July 1, 2020 by xi'an

The weekly puzzle from Le Monde is a leaking variant on an old puzzle:

Three buckets have capacities of 8, 5 and 3 litres, respectively. At the start of the game, the 8 litre bucket is full and both others are empty. Aiming at reaching exactly 4 litres in one bucket, water is transferred between buckets. However, a fraction 1/k is lost with each transfer. If k=9, it is possible to reach 4 litres in three operations? If k=7, is it at all possible to reach 4 litres?

By sheer random search

k=1/5
z=c(8,5,3)
m<-function(s){
  i=sample(1:3,2)
  s[i]=s[i]+ifelse(
   rep((a<-z[i[1]]-s[i[1]])<(b<-s[i[2]])*(1-k),2),
   a*c(1,-1-k),b*c(1-k,-1))
  s}

I found that most fractions allow to reach 4 litres starting with k=2. (And am unsure the missing ones, like 18 or 21 are not due to a lack of luck… In particular, for k=9, the shortest path is

 8.000    0 0
 2.375    5 0
 0.000    5 2.11
 0.000    4 3

sans sérif & sans chevron

Posted in Books, R, Statistics, University life with tags , , , , , , , , , , , , on June 17, 2020 by xi'an
{\sf df=function(x)2*pi*x-4*(x>1)*acos(1/(x+(1-x)*(x<1)))}

As I was LaTeXing a remote exam for next week, including some R code questions, I came across the apparent impossibility to use < and > symbols in the sans-sérif “\sf” font… Which is a surprise, given the ubiquity of the symbols in R and my LaTeXing books over the years. Must have always used “\tt” and “\verb” then! On the side, I tried to work with the automultiplechoice LaTeX package [which should be renamed velomultiplechoice!] of Alexis Bienvenüe, which proved a bit of a challenge as the downloadable version contained a flawed file of automultiplechoice.sty! Still managed to produce a 400 question exam with random permutations of questions and potential answers. But not looking forward the 4 or 5 hours of delivering the test on Zoom…

overlap, overstreched

Posted in Books, Kids, R, Statistics with tags , , , , , , on June 15, 2020 by xi'an

An interesting challenge on The Riddler on the probability to see a random interval X’ing with all other random intervals when generating n intervals from Dirichlet D(1,1,1). As it happens the probability is always 2/3, whatever n>1, as shown by the R code below (where replicate cannot be replaced by rep!):

qro=function(n,T=1e3){
  quo=function(n){
     xyz=t(apply(matrix(runif(2*n),n),1,sort))
  sum(xyz[,1]<min(xyz[,2])&xyz[,2]>max(xyz[,1]))<0}
  mean(replicate(quo(n),T))}

and discussed more in details on X validated. As only a property on permutations and partitions. (The above picture is taken from this 2015 X validated post.)

Le Monde puzzle [#1147]

Posted in Books, Kids, R with tags , , , , , , on June 10, 2020 by xi'an

The weekly puzzle from Le Monde is not much, again:

A number A is such that (1) the difference between two digits is never 1 (2) two digits are equal to 2 (3) it only involves 3 different digits (4) it is a multiple of 4 (5)  the sum of two of the digits is 5 (6) the product of the digits is not a multiple of 6 (7) the digits are between 0 and 6. What are the values of A with no digit equal to 0? What is the smallest A containing a zero?

as brute force produced 2052 as the smallest number with a zero. But I could not find numbers without a zero which seems indeed impossible since (1) plus (2) excludes 1 and 3, then (5) implies 0 and 5 are the other digits. Did I misunderstood the wording?!

Rereading the puzzle pointed out a detail I had missed:

A digit 1<x<7 is part of the digits of A if and only if (x) is true.

Which of course reduces the number of constraints. Using the checking function (and possibly calling xor for the first time ever!)

rule<-function(A){
  xor(!1%in%outer(A,A,'-'),!1%in%A)&
    xor(sum(A==2)==2,!2%in%A)&
    xor(length(unique(A))==3,!3%in%A)&
    xor(!dig2num(A)%%4,!4%in%A)&
    xor(5%in%outer(A,A,'+'),!5%in%A)&
    xor(!!prod(A)%%6,!6%in%A)}

the first realisations of A are

  [1]   304   340   344  2452  2524  3004  3040  3044  3304  3340  3344
 [12]  3400  3404  3440  3444  4252  4300  4304  4340  4344  5224 20452
 [23] 20524 22335 22353 22355 22504 22533 22535 22540 22553 23235 23253
 [34] 23255 23325 23523 23525 24052 24520 25024 25204 25233 25235 25240
 [45] 25253 25323 25325 25420 25523 30004 30040 30044 30304 30340 30344
 [56] 30400 30404 30440 30444 32235 32253 32255 32325 32523 32525 33004
 [67] 33040 33044 33225 33304 33340 33400 33404 33440 33522 34000 34004
 [78] 34040 34044 34300 34304 34340 34400 34404 34440 35223 35225 35322
 [89] 35522 40252 40300 40304 40340 40344 42052 42520 43000 43004 43040
[100] 43044 43300 43304 43340 43400 43404 43440 44300 44304 44340 45220
[111] 50224 52024 52204 52233 52235 52240 52253 52323 52325 52420 52523
[122] 53223 53225 53322 53522 54220 55223 55322

with the first value containing 0 being 304. And no 6 ever appearing in the numbers!