Archive for matrix

spiral matrix [X-validated]

Posted in Books, Kids, R with tags , , , , , on February 11, 2022 by xi'an

One recent code-golf challenge was to write the shortest possible code representing the first n² integers in a spiral progression, e.g.,

 0  1  2  3 4
15 16 17 18 5
14 23 24 19 6
13 22 21 20 7
12 11 10  9 8 

While I did not come close to the best R code (with 67 bytes), this proved an interesting coding exercise, looking for a way to rotate a matrix in R, and then filling one batch at a time. Here is my clumsy if original (?) R output exploiting the vector representation of matrices in R:

r=function(x)apply(t(x),2,rev) #-90⁰ rotation
w=which;m=min
X=diag(0,n)
X[1:n]=n:1
while(!m(X)){
  X=r(X)
  i=m(w(!X))
  j=w(!!X)
  j=m(j[j>i])-1
  X[i:j]=-m(-X)+(j-i+1):1}
while(X[1]>1)X=r(X)

although I later found a webpage proposing (non-optimised) solutions in most computer languages. Thanks to Robin’s remarks, a tighter version is

r=function(n){
w=which
X=diag(0,n)
X[n,]=m=n:1
while(!min(X)){
i=w(!X)[1]
j=w(!!X[-1:-i])
X[i-1+1:j]=max(X)+j:1       #produces warnings
X=t(X)[m,]}                     #-90⁰ rotation
`if`(n%%2,t(t(X)[m,])[m,],X)-1} #180⁰ rotation

Le Monde puzzle [#738]

Posted in R with tags , , , , , on September 2, 2011 by xi'an

The Friday puzzle in Le Monde this week is about “friendly perfect squares”, namely perfect squares x2>10 and y2>10 with the same number of digits and such that, when drifting all digits of x2 by the same value a (modulo 10), one recovers y2. For instance, 121 is “friend” with 676. Here is my R code:

xtrct=function(x){
  x=as.integer(x)
  digs=NULL
  for (i in 0:trunc(log(x,10))){
    digs[i+1]=trunc((x-sum(digs[1:i]*10^(trunc(log(x,10)):(trunc(log(x,10))-
    i+1))))/10^(trunc(log(x,10))-i))}
  return(digs)
  }

pdfct=(4:999)^2
for (t in 1:5){
  pfctsq=pdfct[(pdfct>=10^t)&(pdfct<10^(t+1))]
  rstrct=apply(as.matrix(pfctsq),1,xtrct)

  for (i in 1:(dim(rstrct)[2]-2)){

   dive=apply(matrix(rstrct[,(i+1):dim(rstrct)[2]]-
   rstrct[,i],nrow=t+1),2,unique)
    if (is.matrix(dive))
       dive=lapply(seq_len(ncol(dive)), function(i) dive[,i])
    dive=as.integer(lapply(dive,length))
    if (sum(dive==1)>0)
       print(c(pfctsq[i],pfctsq[
       ((i+1):dim(rstrct)[2])[(dive==1)]]))
    }
  }

which returns

[1] 121 676
[1] 1156 4489
[1] 2025 3136
[1] 13225 24336
[1] 111556 444889

namely the pairs (121,676), (1156,4489), (2025,3136), (13225,24336), and (111556,444889) as the solutions. The strange line of R code

    if (is.matrix(dive))
       dive=lapply(seq_len(ncol(dive)), function(i) dive[,i])

is due to the fact that, when the above result is a matrix, turning it into a list means each entry of the matrix is an entry of the list. After trying to solve the problem on my own for a long while (!), I found the above trick on stackoverflow. (As usual, the puzzle is used as an exercise in [basic] R programming. There always exists a neat mathematical solution!)