## Le Monde puzzle [#1111]

Posted in R, Statistics with tags , , , , , , on September 18, 2019 by xi'an

Another low-key arithmetic problem as Le Monde current mathematical puzzle:

Notice that there are 10 numbers less than, and prime with 11, 100 less than and prime with 101, 1000 less than, and prime with 1111? What is the smallest integer N such that the cardinal of the set of M<N prime with N is 10⁴? What is the smallest multiple of 1111 using only different digits? And what is the largest?

As it is indeed a case for brute force resolution as in the following R code

```library(numbers)
homanycoprim=function(n){
many=1
for (i in 2:(n-1)) many=many+coprime(i,n)
return(many)}

smallest=function(){
n=1e4
many=homanycoprim(n)
while (many!=1e4) many=homanycoprim(n<-n+1)
return(n)}
```

which returns n=10291 as the smallest value of N.  For the other two questions, the usual integer2digit function is necessary

```smalldiff=function(){
n=1111;mul=1
while (mul<1e6) {
x=as.numeric(strsplit(as.character(n*mul), "")[[1]])
while (sum(duplicated(x))!=0){
mul=mul+1
x=as.numeric(strsplit(as.character(n*mul), "")[[1]])}
print(n*mul);mul=mul+1}}
```

leading to 241,087 as the smallest and 9,875,612,340 as the largest (with 10 digits).

Posted in Kids, R with tags , , , on July 6, 2018 by xi'an

Another quick riddle from the riddler: solve the equation

EXMREEK + EHKREKK = ?K?H?X?E

which involves every digit between 0 and 9. While the puzzle can be unravelled by considering first E and K, which must be equal to 6 and 3, a simple R code also leads to the conclusion

```isok <- function(a,b){
s=as.numeric(unlist(strsplit(as.character(sum(10^(6:0)*a)+
sum(10^(6:0)*b)),"")))
if (length(s)==7){ goal=FALSE}else{
goal=(length(unique(c(a,b,s)))==10)&(a[2]==s[6])&
(s[8]==a[6])&(s[2]==a[7])&(b[2]==s[4])}
return(goal)}

pasok <- function(T=1e3){
for (t in 1:T){
a[1]=a[5]=a[6]=6;a[7]=3
digs=sample(c(0:2,4,5,7:9),4)
a[2:4]=digs[1:3] b[1]=a[1];b[2]=digs[4];
b[3]=a[7];b[4]=a[4];b[5]=a[1];b[6:7]=a[7]
if (isok(a=a,b=b))
print(rbind(a,b))}}

> pasok()
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
a    6    2    4    7    6    6    3
b    6    8    3    7    6    3    3
```

which sum is 13085296.

## Le Monde puzzle [#939bis]

Posted in Books, Kids, R with tags , , , , , , , on December 18, 2015 by xi'an

If you remember the previous post, I had two interpretations about Le Monde mathematical puzzle #639:

Find all integers with less than 11 digits that are perfect squares and can be written as a(a+6), a being an integer.

and:

Find all integers with less than 11 digits that are perfect squares and can be written as x concatenated with (x+6), x being an integer.

I got a nice email from Jamie Owen, from Newcastle, Britain, about an R resolution with a clever code, as opposed to mine!

About the second version of the puzzle, Jamie first creates the vector of concatenations:

```
x = 1:1e5
cats = x * (10^floor(log10(x+6) + 1) +1)+ 6
```

He then made the function perfect more… perfect:

```perfect=function(b){
a=trunc(sqrt(b))
any((a:(a+1))^2 == b)
}
```

(using a function any() I had not seen before, and then got the collection of solutions as

```x = 1:1e5
x[sapply(x * (10^floor(log10(x+6) + 1) +1)+ 6,perfect)]
[1] 15 38
```

which runs about 25 times faster than my R solution! (And he further designed a 100 times faster version…)

Jamie also proposed an R code for solving the first version of that puzzle:

```max = 1e10
squares = (1:floor(sqrt(max)))^2
a = -1e6:1e6
# which squares have solutions
sols = intersect(a*(a + 6), squares)
# what are they?
f = function(x){
power = floor(floor(log10(x))/2)+1
a = -10^power:10^power
sols = c(x,a[a*(a+6) - x == 0])
names(sols) = c("square", "a1", "a2")
sols
}
sapply(sols,f)
## [,1]
## square 16
## a1 -8
## a2 2
```

which returns again 2 as the unique positive solution (equivalent to -8, if considering relative integers). A great lesson in efficient R programming, thanks Jamie!

## Le Monde puzzle [#939]

Posted in Books, Kids, R with tags , , , , , , on December 11, 2015 by xi'an

A Le Monde mathematical puzzle about special integers:

Find all integers with less than 11 digits that are perfect squares and can be written as a(a+6), a being an integer.

Eleven digits being too much for a brute force exploration of the form `for (t in 1:1e11)`…, some preliminary  analysis is needed, but I could not figure out a reason why there is no solution apart from 2… (I checked up to 1e8!)

Since I had “guessed” the above puzzle from the solution published one week later (!), I checked the quality of my guesswork with my friend Jean-Louis Fouley, who gave me the genuine question, based on a different interpretation of a(a+6):

Find all integers with less than 11 digits that are perfect squares and can be written as x concatenated with (x+6), x being an integer.

This is more open to brute-force R exploration (with some help from stack overflow) since x only has five digits at most!

```
perfect=function(b){
x=FALSE
a=trunc(sqrt(b))
for (i in a:(a+1))
if (i^2==b) x=TRUE
return(x)}

for (x in 1:(1e6-1))
if (perfect(
as.numeric(paste(c(as.numeric(strsplit(as.character(x), "")[[1]]),
as.numeric(strsplit(as.character(x+6), "")[[1]])),collapse=""))))
print(x)
```

Which returns

```[1] 15
[1] 38
```

and then crashes for x=99994, because

```strsplit(as.character(1e+05), "")
```

does not return the six digits of 1e+05 but

```[[1]]
[1] "1" "e" "+" "0" "5"
```

instead. Except for this value of x, no other solution is found using this R code. And for x=99994, y=99994100000 is not a perfect square.

## Le Monde puzzle [#904.5]

Posted in Books, Kids, R, Statistics, University life with tags , , , on March 25, 2015 by xi'an

Find all plural integers, namely positive integers such that (a) none of their digits is zero and (b) removing their leftmost digit produces a dividing plural integer (with the convention that one digit integers are all plural).

a slight modification in the R code allows for a faster exploration, based on the fact that solutions add one extra digit to solutions with one less digit:

First, I found this function on Stack Overflow to turn an integer into its digits:

```pluri=plura=NULL
#solutions with two digits
for (i in 11:99){

dive=rev(digin(i)[-1])
if (min(dive)&gt;0){
dive=sum(dive*10^(0:(length(dive)-1)))
if (i==((i%/%dive)*dive))
pluri=c(pluri,i)}}

for (n in 2:6){ #number of digits
plura=c(plura,pluri)
pluro=NULL
for (j in pluri){

for (k in (1:9)*10^n){
x=k+j
if (x==(x%/%j)*j)
pluro=c(pluro,x)}
}
pluri=pluro}
```

which leads to the same output

```&gt; sort(plura)
[1] 11 12 15 21 22 24 25 31 32 33 35 36
[13] 41 42 44 45 48 51 52 55 61 62 63 64
[25] 65 66 71 72 75 77 81 82 84 85 88 91
[37] 92 93 95 96 99 125 225 312 315 325 375 425
[49] 525 612 615 624 625 675 725 735 825 832 912
[61] 915 925 936 945 975 1125 2125 3125 3375 4125
[70] 5125 5625
[72] 6125 6375 7125 8125 9125 9225 9375 53125
[80] 91125 95625
```