Tribonacci sequence

Posted in Books, Kids, R with tags , , , , , on January 3, 2023 by xi'an

A simplistic puzzle from The Riddler when applying brute force:

A Tribonacci sequence is based on three entry integers a ≤ b ≤ c, and subsequent terms are the sum of the previous three. Among Tribonacci sequences containing 2023, which one achieves the smallest fourth term, a+b+c ?

The R code

tri<-function(a,b,e){
while(F<2023){
F=a+b+e;a=b;b=e;e=F}
return(F<2024)}
sol=NULL;m=674
for(a in 1:m)
for(b in a:m)
for(e in b:m)
if(tri(a,b,e)){
sol=rbind(sol,c(a,b,e))}


leads to (1,1,6) as the solution… Incidentally, this short exercise led me to finally look for a fix to entering vectors as arguments of functions requesting lists:

do.call("tri",as.list(sol[2023,]))


another drawer of socks

Posted in Books, Kids, R, Statistics with tags , , , , , , on November 6, 2022 by xi'an

A socks riddle from the Riddler but with no clear ABC connection! Twenty-eight socks from fourteen pairs of socks are taken from a drawer, one by one, and laid on a surface that only fit nine socks at a time, with complete pairs removed. What is the probability that all pairs are stored without running out of space? No orphan socks then!!

Writing an R code for this experiment is straightforward

for(v in 1:1e6){
S=sample(rep(1:14,2))
x=S[1]
for(t in 2:18){
if(S[t]%in%x){x=x[S[t]!=x]}else{x=c(x,S[t])}
if(sum(!!x)>9){
F=F+1;break()}}}


and it returns a value quite close to 0.7 for the probability of success. I was expecting a less brute-force resolution but the the Riddler only provided the answer of 70.049 based on the above tree of probabilities (which I was too lazy to code).

Posted in Books, Kids, pictures, Statistics with tags , , , , , on November 1, 2022 by xi'an

An easy riddle, riding the birthday paradox. Namely how many people on average have to sequentially enter a room for a double birthday to occur?

$\sum_{n=2}^{366}n\prod_{m=1}^{n-2}\left(1-\frac{m}{365}\right)\frac{n-1}{365}$

as only the last person in can share a birthday with those already in. This sum equals 24.62. Since the “magic number” is 23, this may sound wrong but the median attached to the above distribution is truly 23. My R code is

q=rep(1,a<-366)/365
for(n in 3:a)q[n]=q[n-1]*(1+1/(n-2)-(n-1)/365)
sum(q[-1]*(2:a))


optimal Gaussian zorbing

Posted in Books, Kids, R, Statistics with tags , , , , , , on August 30, 2022 by xi'an

A zorbing puzzle from the Riddler: cover the plane with four non-intersecting disks of radius one towards getting the highest probability (under the standard bivariate Normal distribution).

As I could not see a simple connection between the disks and the standard Normal, beyond the probability of a disk being given by a non-central chi-square cdf (with two degrees of freedom), I (once again) tried a random search by simulated annealing, which ended up with a configuration like the above, never above 0.777 using a pedestrian R code like

for(t in 1:1e6){# move the disk centres
Ap=A+vemp*rnorm(2)
Bp=B+vemp*rnorm(2)
while(dist(rbind(Ap,Bp))<2)Bp=B+vemp*rnorm(2)
Cp=C+vemp*rnorm(2)
while(min(dist(rbind(Ap,Bp,Cp)))<2)Cp=C+vemp*rnorm(2)
Dp=D+vemp*rnorm(2)
while(min(dist(rbind(Ap,Bp,Cp,Dp)))<2)Dp=D+vemp*rnorm(2)
#coverage probability
pp=pchisq(1,df=2,ncp=Ap%*%Ap)+pchisq(1,df=2,ncp=Bp%*%Bp)+
pchisq(1,df=2,ncp=Cp%*%Cp)+pchisq(1,df=2,ncp=Dp%*%Dp)
#simulated annealing step
if(log(runif(1))<(pp-p)/sqrt(temp)){
A=Bp;B=Cp;C=Dp;D=Ap;p=pp
if (sol\$val<p) sol=list(val=pp,pos=rbind(A,B,C,D))}
temp=temp*.9999}


I also tried a simpler configuration where all disk centres were equidistant from a reference centre, but this led to a lower “optimal” probability. I was looking forward the discussion of the puzzle, to discover if anything less brute-force was possible! But there was no deeper argument there beyond the elimination of other “natural” configurations (and missing the non-central χ² connection!). Among these options, having two disks tangent at (0,0) were optimal. But the illustration was much nicer:

a genuine riddle

Posted in Books, Kids, pictures with tags , , , , , on August 5, 2022 by xi'an

A riddle from The Riddler that was pure (if straightforward) logic rather than brute force compulation or mathematical modelling:

Four bags of many marbles are labelled R(ed), B(lue), G(green) and μ (mixed), except that all labels are wrong. Given the possibility to draw two balls, one at a time, from any bag, is it possible to select two monochromatic bags?

Bag μ draw is returning a color, R say, as it is a monochromatic bag. Drawing from another color bag, B say, will produce R or B, in which case it is μ, i.e., mixed (polychromatic), which means the other bags are monochromatic, or G. For this last case, bag B is either polychromatic, in which case bag G is made of blue marbles and bag R of green marbles, or monochromatic, in which case bag G is mixed and bag R is full of blue marbles, but monochromatic for either situation, hence to be chosen on top of bag μ.