Archive for unique()

operation precisely impossible

Posted in Books, Kids, R, University life with tags , , , , , , , , , , , , on May 13, 2023 by xi'an

Since the solution to the previous riddle from The Riddler on the maximum of  different terms in the composed operation

a∅b∅c∅d∅e∅f

depending on the bracketing ordering and the meaning of each ∅ among one of the six elementary operations got posted today as 974,860, I got back to my R code to understand why it differed from my figures by two orders of magnitude and realised I was overly trusting the R function unique. As it was returning more “different” entries than it should have, especially when choosing the six starting numbers (a,…,f) as Uniform (0,1). Using integers instead led for instance to 946,558, which was not so far from the target. But still imprecise as to whether or not some entries had been counted several times. I mentioned the issue to Robin, who rose to the challenge and within minutes came up with using the R function almost.unique from the CRAN package bazar, then producing outcomes like 974,513, hence quite close to 974,860 for random initialisations!

Le Monde puzzle [#869]

Posted in Books, Kids, R, Statistics, University life with tags , , , , , , , on April 27, 2014 by xi'an

A Le Monde mathematical puzzle once again in a Sudoku mode:

In an nxn table, all integers between 1 and n appear n times. If max denotes the maximum over the numbers of different integers on all rows and columns,  what is the minimum value of max when n=7? when n=11?

I tried to solve it by the following R code (in a pre-breakfast mode in my Reykjavik Airbnb flat!):

#pseudoku
n=7
T=10^4
vals=rep(1:n,n)
minmax=n
for (t in 1:T){
  psudo=matrix(sample(vals),ncol=n)
  maxc=maxr=max(sapply(apply(psudo,1,unique),length))
  if (maxc<minmax)
     maxr=max(sapply(apply(psudo,2,unique),length))
  minmax=min(minmax,max(maxc,maxr))
  }

but later realised that (a) the

sapply(apply(psudo,1,unique),length)

failed when all rows or all columns had the same number of unique terms and (b) I did not have to run the whole matrix:

vals=rep(1:n,n)
minmax=n
for (t in 1:T){
  psudo=matrix(sample(vals),ncol=n)
  maxc=max(length(unique(psudo[1,])),length(unique(psudo[,1])))
  i=1
  while((i<n)&(maxc<minmax)){
   i=i+1
   maxc=max(maxc,
        length(unique(psudo[i,])),
        length(unique(psudo[,i])))}
  minmax=min(minmax,maxc)
  }

gaining a factor of 3 in the R execution. With this random exploration, the minimum value returned was 2,2,2,3,4,5,5,6,7,8 for n=2,3,4,5,6,7,8,9,10,11. Half-hearted simulating annealing during one of the sessions of AISTATS 2014 did not show any difference…