Archive for 2011

Pluss[ki]es and minuss[ki]es

Posted in Kids, Mountains, pictures, Travel with tags , , , on December 27, 2011 by xi'an

Among the plus[ki]ses of our traditional skiing trip this year, the great company of both extended family and my daughter’s friend,  Julia, some days of superbly warm and sunny weather, skiing black diamonds with my daughter, loads of snow, the reward of the white ridges encasing the valley, my wife’s zen approach to skiing, glimpses of La Meije in good weather, great water, bread and cheese, very few people on the runs, with hardly any short-skier, no queues, a terrific first home-made X’mas dinner (thanks to Jean-Michel et François for their contributions!), rekindling skiing feelings from Park City [after a few days], watching my nephew Paul learning to ski in his analytical way, starting a new book purchased in Provo and unexpectedly mixing fantasy and climbing, playing chess with Paul on evenings and narrowly reaching a stalemate once, a record-time for putting the chains on my car (as opposed to last year!), reading the end of Persi’s book in an Alpine chalet, …

Among the minus[ki]ses, the choice of a terrible rental apartment company and of a mediocre hotel, facing frozen water in one apartment, leading to over-heated apartments the rest of the stay, first days of grey and white weather, straining my thumb in a mound of powdery snow the day before last, loosing any feeling of what’s horizontal and what’s not and falling in a hole as a result [with non-lasting effects!], taking the wrong advice from the instructor during a short course and loosening my ski shoes too much for the first days, watching an appalling movie on the final night about a blogging cashier, my wife’s zen approach to skiing, getting my new book stolen by my son, having to change apartments three times in 8 days,  the wifi at the apartment being incompatible with wordpress,  a not-so-terrific second X’mas dinner at the hotel, wasting 15mn taking my chains off, the final traffic-jam when reaching Paris, …

Le Monde puzzle [#754]

Posted in Mountains, R, Statistics, University life with tags on December 26, 2011 by xi'an

The pre-X’mas puzzle in Le Monde weekend edition is about “magical numbers” having as digits all digits between 0 and n (at least once) and being multiple of all digits between 1 and (n+1). Easy, isn’t it?! I thought so while driving down to the Alps on Saturday and (on Monday early morning) I tried a brute force solution

[sourcecode language=”r” gutter=”false”]
magi=function(n){
prdct=prod(1:(n+1))

for (t in 1:10^6){
num=sum(sample(0:n)*10^(0:n))
if (num==prdct*trunc(num/prdct)){
print(num)
break()}
}
}
[/sourcecode]

which worked for n=2,3, but failed for n=4. Maybe too brute-force? So I imposed the basic divisibility by 2×5=10 from the start, namely the last digit had to be 0:

[sourcecode language=”r” gutter=”false”]
magi=function(n){
prdct=prod(1:(n+1))
if (n>3){
for (t in 1:10^6){
num=sum(sample(0:n)*10^(0:n))
if (num==prdct*trunc(num/prdct)){
print(num)
break()}
}
}else{
for (t in 1:10^6){
num=10*sum(sample(1:n)*10^(0:(n-1)))
if (num==prdct*trunc(num/prdct)){
print(num)
break()}
}
}
}
[/sourcecode]

Still did not work for n=4… Actually, there was a mistake in prdct in both of the above, in that the solution number only needs to be divided by the largest powers to the prime numbers between 2 and n+1, as implemented below.

I thought a wee more on the conditions and realised that any random permutation of the digits {1,…,4} could not be divided by 3, since their sum is 10. Therefore, the solution for n=4 must have at least 6 digits. This led me to a more general R function which works for the cases n=4,5,6 and has a second parameter, k, namely the number of digits in the proposed solution. It also includes imposing a correct second digit based on the divisibility by 4:

[sourcecode language=”r” gutter=”false”]
magi456=function(n,k){

proo=3*4*5*(1+6*(n==6)) #only required divisors
digi=10^(0:(k+1))
sol=rep(9,k+2)*digi
for (t in 1:10^6){

a0=0 #multiple of 2*5
a1=sample(2*(1:trunc(n/2)),1) #multiple of 4
b=sample((1:n)[-a1]) #all other integers
b=sample(c(b,sample(0:n,(k-n+1),rep=TRUE)))
a=sum(c(a0,a1,b)*digi)
if (a%%proo==0){
sol=a
print(a)
break()
}

}

return(sol)
}
[/sourcecode]

A similar solution can be proposed for the cases n=7,8,9, with a different constraint on the three first digits due to the divisibility by 8. There again is a special case when n=7, since the sum of all integers from 1 to 7 is 28, not divisible by 3. On the other hand, the sum of all integers from 1 to 8 is 36 and the sum of all integers from 1 to 9 is 45, both divisible by 9. Here is the corresponding R code:

[sourcecode language=”r” gutter=”false”]
magi789=function(n,k){

proo=(1+2*(n==7))*5*7*8*(1+8*(n>7)) #only required divisors
digi=10^(0:(k+1))
sol=rep(9,k+2)*digi

for (t in 1:10^6){
a0=0 #multiple of 2*5
#multiple of 8 (4, 2)
a1=sample(2*(1:trunc(n/2)),1)
a2=sample((1:n)[-a1],1)
while ((a1+2*a2)%%4!=0){
a1=sample(2*(1:trunc(n/2)),1)
a2=sample((1:n)[-a1],1)
}
b=sample((1:n)[-c(a1,a2)]) #all other integers
b=sample(c(b,sample(0:n,(k-n+1),rep=TRUE)))
a=sum(c(a0,a1,a2,b)*digi)
if (a%%proo==0){
sol=a
print(a)
break()
}

}

return(sol)
}
[/sourcecode]

Le Monde was furthermore asking for the smallest solution for each n. I ran the R code a few thousand times for every n and obtained

[sourcecode language=”r” gutter=”false”]
> magi456(4,4)
122340
> magi456(5,4)
123540
> magi456(6,5)
1235640
> magi789(7,7)
122437560
> magi789(8,7)
123487560
> magi789(9,8)
1234759680
[/sourcecode]

Of course, these are upper bounds on the smallest solutions (and there are more clever ways of R coding the above as well as of solving the puzzle in a mathematical way). Being away till Tuesday, I will not check the solution till then…

art brut

Posted in pictures with tags , , on December 25, 2011 by xi'an

from my office

Posted in pictures, University life with tags , , , , on December 24, 2011 by xi'an

Obnoxious plus

Posted in pictures, Travel with tags , , , on December 23, 2011 by xi'an

On the flight back to Paris on Thursday, there were three very loud and incredibly obnoxious (French) executives, speaking to one another across seats on different aisles as if they were alone… Sitting right behind me. Even the noise cancelling headphone was not up to it! They seemed to be part of a media company as they were discussing buying the rights for the Dakar rally. Just as loudly. Among the two hours of highly cerebral conversation, here are a few gems (others were either too crude or too prone to libel!) from the top protagonist:

“I prohibited my cleaning lady to use […] for my shirts, it is chemical, it goes right into the skin.”

…

“No, no, Espéranza only does the cleaning. Manuela does the ironing.”

…

“She always complains about women hair on my couch. “

…

“You should buy those yellow latex overshoes. The salt they put to clean the sidewalks are killing your leather shoes, no matter how good they are.”