## Le Monde puzzle [#842]

Posted in Books, Kids, R with tags , , , on November 30, 2013 by xi'an

An easily phrased (and solved?) Le Monde mathematical puzzle that does not [really] require an R code:

The five triplets A,B,C,D,E are such that

$A_1=B_2+C_2+D_2+E_2\,,\ B_1=A_2+C_2+D_2+E_2\,,...$

and

$A_2=B_3+C_3+D_3+E_3\,,\ B_2=A_3+C_3+D_3+E_3\,,...$

Given that

$A_1=193\,,\ B_1=175\,, C_1=185\,, D_1=187\,, E_1<175$

find the five triplets.

Adding up both sets of equations shows everything solely depends upon E1… So running an R code that checks for all possible values of E1 is a brute-force solution. However, one must first find what to check. Given that the sums of the triplets are of the form (16s,4s,s), the possible choices for E1 are necessarily restricted to

```> S0=193+187+185+175
> ceiling(S0/16)
[1] 47
> floor((S0+175)/16)
[1] 57
> (47:57)*16-S0 #E1=S1-S0
[1]  12  28  44  60  76  92 108 124 140 156 172
```

The first two values correspond to a second sum S2 equal to 188 and 192, respectively, which is incompatible with A1 being 193. Furthermore, the corresponding values for E2 and E3 are then given by

```> S2==(49:57)*4
> E1=(49:57)*16-S0
> E2=S2-E1
> S3=S2/4
> S3-E2
[1] -103  -90  -77  -64  -51  -38  -25  -12    1
```

which excludes all values but E1=172. No brute-force in the end…

## Buffon needled R exams

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

Here are two exercises I wrote for my R mid-term exam in Paris-Dauphine around Buffon’s needle problem. In the end, the problems sounded too long and too hard for my 3rd year students so I opted for softer questions. So recycle those if you wish (but do not ask for solutions!)

## Le Monde puzzle [#840]

Posted in Books, Kids, R with tags , , , , on November 23, 2013 by xi'an

Another number theory Le Monde mathematical puzzles:

Find 2≤n≤50 such that the sequence {1,…,n} can be permuted into a sequence such that the sum of two consecutive terms is a prime number.

Now this is a problem with an R code solution:

```library(pracma)
foundsol=TRUE
N=2
while (foundsol){

N=N+1
noseq=TRUE
uplim=10^6
t=0
while ((t<uplim)&&(noseq)){

randseq=sample(1:N)
sumseq=randseq[-1]+randseq[-N]
noseq=min(isprime(sumseq))==0
t=t+1
}

foundsol=!noseq
if (!noseq){
lastsol=randseq}else{ N=N-1}
}
```

which returns the solution as

```> N
[1] 12
> lastsol
[1]  6  7 12 11  8  5  2  1  4  3 10  9
```

and so it seems there is no solution beyond N=12…

However, reading the solution in the next edition of Le Monde, the authors claim there are solutions up to 50. I wonder why the crude search above fails so suddenly, between 12 and 13! So instead I tried a recursive program that exploits the fact that subchains are also verifying  the same property:

```findord=function(ens){

if (length(ens)==2){
sol=ens
foundsol=isprime(sum(ens))}
else{
but=sample(ens,1)
nut=findord(ens[ens!=but])
foundsol=FALSE
sol=ens
if (nut\$find){
tut=nut\$ord
foundsol=max(isprime(but+tut[1]),
isprime(but+tut[length(tut)]))
sol=c(tut,but)
if (isprime(but+tut[1]))
sol=c(but,tut)
}
}
list(find=foundsol,ord=sol)
}
```

And I ran the R code for N=13,14,…

```> stop=TRUE
> while (stop){
+   a=findord(1:N)
+   stop=!(a\$find)}
```

until I reached N=20 for which the R code would not return a solution. Maybe the next step would be to store solutions in N before moving to N+1. This is just getting  me too far from a mere Saturday afternoon break.

## Bayesian essentials with R available on amazon

Posted in Books, R, Statistics, University life with tags , , , , , on November 14, 2013 by xi'an

Bayesian Essentials with R is now available both as an e-book and as a hardcover book on amazon.com!

## Le Monde puzzle [#838]

Posted in Books, Kids, R with tags , , , , , , , , , , on November 2, 2013 by xi'an

Another one of those Le Monde mathematical puzzles which wording is confusing to me:

The 40 members of the Academy vote for two prizes. [Like the one recently attributed to my friend and coauthor Olivier Cappé!] Once the votes are counted for both prizes, it appears that the total votes for each of the candidates take all values between 0 and 12. Is it possible that two academicians never pick the same pair of candidates?

I find it puzzling… First because the total number of votes is then equal to 78, rather than 80=2 x 40. What happened to the vote of the “last” academician? Did she or he abstain? Or did two academicians abstain on candidates for only one prize each?  Second, because of the incertitude in the original wording: can we assume with certainty that each integer between 0 and 12 is only taken once? If so, it would mean that the total number of candidates to the prizes is equal to 13. Third, the question seems unrelated with the “data”: since sums only are known, switching the votes of academicians Dupond and Dupont for candidates Durand and Martin in prize A (or in prize B) does not change the number of votes for Durand and Martin.

If we assume that each integer between 0 and 12 only appears once in the collection of the sums of the votes and that one academician abstained on both prizes, the number of candidates for one of the prizes can vary between 4 and 9, with compatible solutions provided by this R line of code:

```N=5
ok=TRUE
while (ok){
prop=sample(0:12,N)
los=(1:13)[-(prop+1)]-1
ok=((sum(prop)!=39)||(sum(los)!=39))}
```

which returns solutions like

```> N=5
> prop
[1]  9 11  7 12
> los
[1]  0  1  2  3  4  5  6  8 10
```

but does not help in answering the question!

Now, with Robin‘s help, (whose Corcoran memorial prize I should have mentioned in due time!), I reformulate the question as

The 40 members of the Academy vote for two prizes. Once the votes are counted for both prizes, it appears that all values between 0 and 12 are found among the total votes for each of the candidates. Is it possible that two academicians never pick the same pair of candidates?

which has a nicer solution: since all academicians have voted there are two extra votes (40-38), meaning either twice 2 or thrice 1. So there are either 14 or 15 candidates ex toto.  With at least 4 for a given prize. I then checked whether or not the above event could occur, using the following (pedestrian) R code:

```for (t in 1:10^3){
#pick number of replicae
R=sample(1:2,1); cand=13+R
#pick number of literary candidates
N=sample(4:(cand-4),1)
if (R==2){
}else{
#correct number of votes
ok=TRUE
while (ok){
drop=sample(1:cand,N)
ok=((sum(prop)!=40)||(sum(los)!=40))
}
#individual votes for scientific candidates
pool=NULL
for (j in 1:N)
pool=c(pool,rep(j,prop[j]))
#individual votes for literary candidates
cool=NULL
for (j in 1:(cand-N))
cool=c(cool,rep(100+j,los[j]))
cool=sample(cool) #random permutation