Archive for allPerms

Le Monde puzzle [#1043]

Posted in Books, Kids with tags , , , , , , on March 5, 2018 by xi'an

An arithmetic Le Monde mathematical puzzle :

A number is “noble” if all its digits are different and if it is equal to the average of all numbers created by permuting its digits. What are the noble numbers?

There is no need for simulation when plain enumeration works. After failing to install the R packge permutations, I installed the R package permute, which works, although (a) the function allPerm does not apply directly to a vector of characters or numbers but only to its size:

> allPerms(c("a","r","h"))
     [,1] [,2] [,3]
[1,]    1    3    2
[2,]    2    1    3
[3,]    2    3    1
[4,]    3    1    2
[5,]    3    2    1

and (b) as seen above the function does not contain “all” permutations since it misses the identity permutation.  Which ends up being fine for solving this puzzle. Using a bit of digit-character manipulation

findzol=function(N=2){
  for (u in 1:(10^N-1)){
    digz=strsplit(as.character(u),"")[[1]]
    if (length(digz)<N) 
      digz=c(rep("0",N-length(digz)),digz)
    if (length(unique(digz))==N){
      permz=apply(matrix(digz[allPerms(1:N)],
             ncol=N),2,as.numeric)
      if (mean(permz%*%10^{(N-1):0})==u) print(u)}}}

I found solutions for N=3

> findzol(3)
[1] 370
[1] 407
[1] 481
[1] 518
[1] 592
[1] 629

and none for N=4,5,6. Le Monde gives solutions for N=9, which is not achievable by my code!