**T**he Friday puzzle in *Le Monde* this week is about “friendly perfect squares”, namely perfect squares *x*^{2}>10 and *y*^{2}>10 with the same number of digits and such that, when drifting all digits of *x*^{2} by the same value a (modulo 10), one recovers *y*^{2}. 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 2:6){
pfctsq=pdfct[(pdfct>=10^t)&(pdfct0)
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!)

Filed under: R Tagged: arithmetics, Le Monde, list, mathematical puzzle, matrix, R

*Related*

To

**leave a comment** for the author, please follow the link and comment on their blog:

** Xi'an's Og » R**.

R-bloggers.com offers

**daily e-mail updates** about

R news and

tutorials on topics such as:

Data science,

Big Data, R jobs, visualization (

ggplot2,

Boxplots,

maps,

animation), programming (

RStudio,

Sweave,

LaTeX,

SQL,

Eclipse,

git,

hadoop,

Web Scraping) statistics (

regression,

PCA,

time series,

trading) and more...

If you got this far, why not

__subscribe for updates__ from the site? Choose your flavor:

e-mail,

twitter,

RSS, or

facebook...

**Tags:** arithmetics, Le Monde, list, mathematical puzzle, matrix, R