Archive for gtools

Le Monde puzzle [#1129]

Posted in Kids, R with tags , , , , , , on February 10, 2020 by xi'an

A number challenge as Le weekly Monde current mathematical puzzle:

When the three consecutive numbers 110, 111 and 112, they all are multiples of the sum of their digits. Are there 4 consecutive numbers with three digits like this? A contrario, does there exist 17 consecutive numbers with three digits such that they cannot be divided by the sum of their digits? 18?

The run of a brute force R search return 510,511,512,513 as the solution to the first question

library(gtools)
bez=!(100:999)%%apply(baseOf(100:999),1,sum)
> (100:897)[bez[-(1:3)]*bez[-c(1:2,900)]*bez[-c(1,899:900)]*bez[-(898:900)]==1]
[1] 510

And to the second one:

> max(diff((1:899)[!!diff(bez)]))
[1] 17

 

Le Monde puzzle [#817]

Posted in Books, Kids, R with tags , , , , on April 19, 2013 by xi'an

The weekly Le Monde puzzle is (again) a permutation problem that can be rephrased as follows:

Find

\min_{\sigma\in\mathfrak{S}_{10}} \max_{0\le i\le 10}\ \sigma_i+\sigma_{i+1}

where \mathfrak{S}_{10} denotes the set of permutations on {0,…,10} and \sigma_i is defined modulo 11 [to turn {0,…,10} into a torus]. Same question for

\min_{\sigma\in\mathfrak{S}_{10}} \max_{0\le i\le 10}\ \sigma_i+\sigma_{i+1}+\sigma_{i+2}

and for

\min_{\sigma\in\mathfrak{S}_{10}} \max_{0\le i\le 10}\ \sigma_i+\cdots+\sigma_{i+5}

This is rather straightforward to code if one adopts a brute-force approach::

[sourcecode language=”r” gutter=”false”]
perminmax=function(T=10^3){
mins=sums=rep(500,3)
permin=matrix(0:10,ncol=11,nrow=3,byrow=TRUE)

for (t in 1:T){
perms=sample(0:10)
adde=perms+perms[c(11,1:10)]
sums[1]=max(adde)
adde=adde+perms[c(10,11,1:9)]
sums[2]=max(adde)
adde=adde+perms[c(9:11,1:8)]+perms[c(8:11,1:7)]
sums[3]=max(adde)
for (j in 1:3)
if (sums[j]<mins[j]){
mins[j]=sums[j];permin[j,]=perms}
}

print(mins)
print(permin)
}
[/sourcecode]

(where I imposed the first term to be zero because of the invariance by permutation), getting the solutions

[sourcecode language=”r” gutter=”false”]
> perminmax(10^5)
[1] 11 17 28
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
[1,]    0   10    1    6    5    4    7    3    8     2     9
[2,]    0   10    4    3    5    1    9    6    2     8     7
[3,]    0    2    9    6    7    3    1    4   10     8     5
[/sourcecode]

for 2, 3, and 5 terms.  (Since 10! is a mere 3.6 million, I checked with an exhaustive search, using the function permutation from the gtools package.)