Archive for mathematical puzzle

Le Monde puzzle [#947]

Posted in Books, Kids, pictures, R, Statistics with tags , , , , , , , on February 2, 2016 by xi'an

Another boardgame in Le Monde mathematical puzzle :

Given an 8×8 chequerboard,  consider placing 2×2 tiles over this chequerboard until (a) the entire surface is covered and (b) removing a single 2×2 tile exposes some of the original chequerboard. What is the maximal number of 2×2 tiles one can set according to this scheme? And for a 10×10 chequerboard?

This puzzle reminded me of Wilfrid Kendall’s introduction to perfect sampling  with leaves seen through a ceiling window falling from above, until sky was no longer visible. The representation was used by Wilfrid to explain that coupling from the past did not need to go all the way back to infinity:

Defining a random coverage of the chequerboard by those 2×2 tiles amounts to selecting a random permutation þ of 1:(n-1)² and finding the subvector of þ producing a full coverage

 grid=matrix(0,n,n)
 path=sample(1:(n-1)^2) #random permutation
 path=path+((path-1)%/%(n-1)) #account for border shift
 i=1
 neigh=c(0,1,n,n+1)
 while (min(grid)==0){ #all entries covered
   grid[path[i]+neigh]=grid[path[i]+neigh]+1
   i=i+1
 }
 i=i-1

Then removing superfluous tiles:

for (k in sample(1:i)){
 krid=grid
 krid[path[k]+neigh]=krid[path[k]+neigh]-1
 if (min(krid)>0){ #off used below
   off[k]=FALSE; grid=krid} #useless tile
 }

And checking the remaining ones are truly essential:

mingrid=0
for (k in (1:i)[off]){
 krid=grid
 krid[path[k]+neigh]=krid[path[k]+neigh]-1
 mingrid=max(mingrid,min(krid))
 }
sky=(mingrid>0) #rejection of the grid

leads to the maximum number of tiles to be [at least] M=16,25,37,54 for n=6,8,10,12…

Le Monde puzzle [#945]

Posted in Books, Kids, pictures, Statistics, Travel, University life with tags , , , , on January 25, 2016 by xi'an

A rather different Le Monde mathematical puzzle:

A two-person game is played on an nxn grid filled with zero. Each player pick an entry, which is increased by one as well as all adjacent entries. The game stops when all entries are equal. For n=3,4,5, what are the possible grids with identical values all over?

If I define an R neighbourhood function

 nighbo=function(i,n){
   neigh=i
   if (i%%n!=1) neigh=c(i-1,neigh)
   if (i%%n>0) neigh=c(i+1,neigh)
   if (i+n<=n*n) neigh=c(i+n,neigh)
   if (i-n>0) neigh=c(i-n,neigh) 
   return(neigh)}

and try a brute force filling of the grid

while ((min(grid)==0)||(length(unique(grid))>1)){
  ent=sample(1:(n*n),1,prob=1/as.vector(grid+1)^10)
  grid[nighbo(ent,n)]=grid[nighbo(ent,n)]+1}

the loop never stops. When thinking of the case n=3 [while running in the early morning], I wondered whether or not reaching an equal value on all entries was at all possible. Indeed, it is impossible to update one of the four corners without updating at least one of the neighbours, while the converse is false. Experimenting further with simulated annealing to optimise the probabilities of picking different entries in the table when n=4,5 seems to indicate this is also the case for larger values of n, in that all attempts lead to larger values of neighbours to the four corners :

outer=c(1,n,n*n,n*n-n+1)
border=sort(unique(c(2:(n-1),(n*n-n+2):(n*n-1),1+n*(1:(n-2)),n+n*(1:(n-2)))))
inner=(1:(n*n))[-c(outer,border)]
#
target=function(weit){
 grid=matrix(0,n,n)
 for (t in 1:1e4){
   cas=sample(1:3,1,prob=weit)
   if (cas==1) ent=sample(outer,1,prob=max(grid[outer])-grid[outer]+1)
   if (cas==2) ent=sample(border,1,prob=max(grid[border])-grid[border]+1)
   if (cas==3) ent=sample(inner,1,prob=max(grid[inner])-grid[inner]+1)
   ent=nighbo(ent,n)
   grid[ent]=grid[ent]+1}
 ave=c(mean(grid[outer]),mean(grid[border]),mean(grid[inner]))
 return(list(dive=max(diff(sort(ave))),ave=ave,ava=mean(grid)))
 }
#
weit=rep(1,3)
cur=target(weit)
T=100
while (cur$dive>0){
 ind=sample(1:3,1,prob=1/cur$ave)
 peit=weit
 peit[ind]=weit[ind]-(cur$ave[ind]-cur$ava)*runif(1)/(cur$ava)
 while(peit[ind]<0)
 peit[ind]=weit[ind]-(cur$ave[ind]-cur$ava)*runif(1)/(cur$ava)
 prop=target(peit)
 if (log(runif(1))*1e4/T<prop$dive-cur$dive){
 weit=peit;cur=prop}
 T=T*1.00001
 print(cur$ave)}

Le Monde puzzle [#944]

Posted in Books, Kids, pictures, Statistics, Travel, University life with tags , , , , on January 20, 2016 by xi'an

A completely dull Le Monde mathematical puzzle:

Find all integer pairs (a,b) less than 10⁶ such that a-b=2015 and ab is a perfect square.

If  I write the only condition as the function

perfect=function(c){
  return(c==trunc(sqrt(c))^2)}

and brute-force checked for all possible solutions

for (A in 2015:1e6)
 if (perfect(A*(A-2015))) print(A)
trunc(y/10)*11+(y-10*trunc(y/10))-(y==10*trunc(y/10))}

which produced

[1] 2015
[1] 2304
[1] 2420
[1] 2511
[1] 3627
[1] 4212
[1] 7056
[1] 7595
[1] 16640
[1] 33759
[1] 41616
[1] 79092
[1] 204020

as the 13 possible answers. If one checks between 10⁶ and 5 10⁶, the only remaining solution is 1,016,064. Which is the highest possible one according to Le Monde.

Le Monde puzzle [#940]

Posted in Books, Kids, pictures, Statistics, Travel, University life with tags , , , on December 25, 2015 by xi'an

A rather different Le Monde mathematical puzzle with no simulation:

A student has x days to train before an exam and decides to take one day off after every nine consecutive days of training. She makes her planning and manages to fit one chapter of her textbook per day of training. She then realises she has forgotten one extra chapter, but cannot keep the pattern, even when taking one day off after 10 consecutive days of training. What is the maximum value of x?

If y denotes the number of chapters, this means that

10⌊(y-1)/9⌋+(y-1-9⌊(y-1)/9⌋)⁻ ≤ x < 11⌊y/10⌋+(y-10⌊y/10⌋)⁻

with  ⌊y/10⌋ denoting the integer part and with (z)⁻ equal to z when z>0 and to -1 when z=0. Defining both functions of y as

curv1 <- function(y){
  10*trunc((y-1)/9)+(y-1-9*trunc((y-1)/9))-(y-1==9*trunc((y-1)/9))}
curv2 <- function(y){
  trunc(y/10)*11+(y-10*trunc(y/10))-(y==10*trunc(y/10))}

I get the following graph:

lemonde941which sees both curves meeting at y=20 or x=21, which could be the answer to the puzzle. However, larger values of x see the above inequality satisfied as well. It is only after y=73 or x=80 that the above inequality is forever impossible since the lower bound is then larger than or equal to the upper bound [when removing the -1 from (z)⁻] or for y=81 or x=88 [when keeping the initial definition of (z)⁻].

Le Monde puzzle [#939bis]

Posted in Books, Kids, R with tags , , , , , , , on December 18, 2015 by xi'an

If you remember the previous post, I had two interpretations about Le Monde mathematical puzzle #639:

Find all integers with less than 11 digits that are perfect squares and can be written as a(a+6), a being an integer.

and:

Find all integers with less than 11 digits that are perfect squares and can be written as x concatenated with (x+6), x being an integer.

I got a nice email from Jamie Owen, from Newcastle, Britain, about an R resolution with a clever code, as opposed to mine!

About the second version of the puzzle, Jamie first creates the vector of concatenations:

 
x = 1:1e5
cats = x * (10^floor(log10(x+6) + 1) +1)+ 6

He then made the function perfect more… perfect:

perfect=function(b){
  a=trunc(sqrt(b))
  any((a:(a+1))^2 == b)
  }

(using a function any() I had not seen before, and then got the collection of solutions as

x = 1:1e5
x[sapply(x * (10^floor(log10(x+6) + 1) +1)+ 6,perfect)]
[1] 15 38

which runs about 25 times faster than my R solution! (And he further designed a 100 times faster version…)

Jamie also proposed an R code for solving the first version of that puzzle:

max = 1e10
squares = (1:floor(sqrt(max)))^2
# possible answers to a(a+6)
a = -1e6:1e6
# which squares have solutions
sols = intersect(a*(a + 6), squares)
# what are they?
f = function(x){
power = floor(floor(log10(x))/2)+1
a = -10^power:10^power
sols = c(x,a[a*(a+6) - x == 0])
names(sols) = c("square", "a1", "a2")
sols
}
sapply(sols,f)
## [,1]
## square 16
## a1 -8
## a2 2

which returns again 2 as the unique positive solution (equivalent to -8, if considering relative integers). A great lesson in efficient R programming, thanks Jamie!

Le Monde puzzle [#939]

Posted in Books, Kids, R with tags , , , , , , on December 11, 2015 by xi'an

A Le Monde mathematical puzzle about special integers:

Find all integers with less than 11 digits that are perfect squares and can be written as a(a+6), a being an integer.

Eleven digits being too much for a brute force exploration of the form `for (t in 1:1e11)`…, some preliminary  analysis is needed, but I could not figure out a reason why there is no solution apart from 2… (I checked up to 1e8!)

Since I had “guessed” the above puzzle from the solution published one week later (!), I checked the quality of my guesswork with my friend Jean-Louis Fouley, who gave me the genuine question, based on a different interpretation of a(a+6):

Find all integers with less than 11 digits that are perfect squares and can be written as x concatenated with (x+6), x being an integer.

This is more open to brute-force R exploration (with some help from stack overflow) since x only has five digits at most!

 
perfect=function(b){
  x=FALSE
  a=trunc(sqrt(b))
  for (i in a:(a+1))
    if (i^2==b) x=TRUE
  return(x)}

for (x in 1:(1e6-1))
  if (perfect(
    as.numeric(paste(c(as.numeric(strsplit(as.character(x), "")[[1]]),
    as.numeric(strsplit(as.character(x+6), "")[[1]])),collapse="")))) 
      print(x)

Which returns

[1] 15
[1] 38

and then crashes for x=99994, because

strsplit(as.character(1e+05), "") 

does not return the six digits of 1e+05 but

[[1]]
[1] "1" "e" "+" "0" "5"

instead. Except for this value of x, no other solution is found using this R code. And for x=99994, y=99994100000 is not a perfect square.

Sunday morning puzzle

Posted in Books, Kids, R with tags , , , on November 22, 2015 by xi'an

A question from X validated that took me quite a while to fathom and then the solution suddenly became quite obvious:

If a sample taken from an arbitrary distribution on {0,1}⁶ is censored from its (0,0,0,0,0,0) elements, and if the marginal probabilities are know for all six components of the random vector, what is an estimate of the proportion of (missing) (0,0,0,0,0,0) elements? 

Since the censoring modifies all probabilities by the same renormalisation, i.e. divides them by the probability to be different from (0,0,0,0,0,0), ρ, this probability can be estimated by looking at the marginal probabilities to be equal to 1, which equal the original and known marginal probabilities divided by ρ. Here is a short R code illustrating the approach that I wrote in the taxi home yesterday night:

#generate vectors
N=1e5
zprobs=c(.1,.9) #iid example
smpl=matrix(sample(0:1,6*N,rep=TRUE,prob=zprobs),ncol=6)
pty=apply(smpl,1,sum)
smpl=smpl[pty>0,]
ps=apply(smpl,2,mean)
cor=mean(ps/rep(zprobs[2],6))
#estimated original size
length(smpl[,1])*cor

A broader question is how many values (and which values) of the sample can be removed before this recovery gets impossible (with the same amount of information).

Follow

Get every new post delivered to your Inbox.

Join 980 other followers