## Le Monde puzzle [#738]

**T**he Friday puzzle in ** Le Monde** this week is about “friendly perfect squares”, namely perfect squares

*x*and

^{2}>10*y*with the same number of digits and such that, when drifting all digits of

^{2}>10*x*by the same value a (modulo 10), one recovers

^{2}*y*. For instance, 121 is “friend” with 676. Here is my R code:

^{2}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 stack**overflow**. (As usual, the puzzle is used as an exercise in [basic] R programming. There always exists a neat mathematical solution!)

*Related*

This entry was posted on September 2, 2011 at 12:11 am and is filed under R with tags arithmetics, Le Monde, list, mathematical puzzle, matrix, R. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

September 7, 2011 at 8:08 am

Hi,

I”m very interested to see if I can get this to work and understand the script. Unofrtunately I get the error message that object ‘rstrct’ is not found. Do you have some idea why that is?

Thanks!

September 7, 2011 at 8:52 am

Rick: Sorry about this. The cut&paste from my vi window to the wordpress editor window is often missing bits, for no clear reason. I hope the code is now complete… (I checked it and it works on my machine!)

September 3, 2011 at 12:14 pm

[…] last, the mathematical puzzles proposed by Buser and Cohen in the weekend edition of Le Monde are available on a dedicated […]

September 2, 2011 at 6:48 pm

I ALMOST tried to beat you to this, as an exercise for my elementary R. Finally, I didn’t make it, this time. However, I did notice that the puzzle starts from 2 figures, so doesn’t [16,49] count?

September 2, 2011 at 7:52 pm

Yes, indeed, I noticed in Le Monde tonight that they had included two figure numbers… Will need to check now!

September 2, 2011 at 8:00 pm

Easy fix: my loop starts with t=2, it should start with t=1! So the first results are

[1] 16 49

[1] 25 36