## Le Monde puzzle [#1133]

Posted in Books, Kids, R with tags , , , , , , on March 28, 2020 by xi'an

A weekly Monde current mathematical puzzle that reminded me of an earlier one (but was too lazy to check):

If ADULE-ELUDE=POINT, was is the largest possible value of POINT? With the convention that all letters correspond to different digits and no digit can start with 0. Same question when ADULE+ELUDE=POINT.

The run of a brute force R search return 65934 as the solution (codegolf welcomed!)

dify<-function(aluda,point)
(sum(aluda*10^(4:0))-sum(rev(aluda)*10^(4:0)))
num2dig<-function(dif) (dif%/%10^(0:4))%%10
sl=NULL
for (t in 1:1e6){
sl=as.matrix(distinct(as.data.frame(sl),.keep_all = TRUE))


where distinct is a dplyr R function.

> 94581-18549
[1] 76032


The code can be easily turned into solving the second question

> 31782+28713
[1] 60495


## Le Monde puzzle [#1134]

Posted in Books, R with tags , , , , , , on March 24, 2020 by xi'an

A Monde mathematical puzzle on gcd’s and scm’s:

If one replaces a pair (a,b) of integers with the pair (g,s) of their greatest common denominator and smallest common multiple, how long at most before the sequence ends. Same question when considering a collection of five integers where two are selected by the pair (g,s) of their greatest common denominator and smallest common multiple.

The first question is straightforward as s is a multiple of s. So the sequence ends at most after one run. For five, run of a brute force R search return 9 as “the” solution (even though the true maximum is 10, as illustrated by the quintuplet (16,24,36,54,81):

ogcd <- function(x,y){r<-x%%y
return(ifelse(r,ogcd(y,r),y))}

oscm<-function(x,y) x*y/ogcd(x,y)

divemul<-function(a,b) return(c(oscm(a,b),ogcd(a,b)))

for (t in 1:1e5){
ini=sample(1:1e2,5)
i=0;per=ker=sample(ini,2)
nez=divemul(per[1],per[2])
while(!max(nez%in%per)){
ini=c(ini[!ini%in%per],nez)
per=sample(ini,2)
ker=rbind(ker,per)
nez=divemul(per[1],per[2])
i=i+1}
sol=max(sol,i)}


## Le Monde puzzle [#1132]

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

A vaguely arithmetic challenge as Le weekly Monde current mathematical puzzle:

Given two boxes containing x and 2N+1-x balls respectively. If one proceeds by repeatedly transferring half the balls from the even box to the odd box, what is the largest value of N for which the resulting sequence in one of the boxes covers all integers from 1 to 2N?

The run of a brute force R search return 2 as the solution

lm<-function(N)
fils=rep(0,2*N)
bol=c(1,2*N)
while(max(fils)<2){
fils[bol[1]]=fils[bol[1]]+1
bol=bol+ifelse(rep(!bol[1]%%2,2),-bol[1],bol[2])*c(1,-1)/2}
return(min(fils))}


with obvious arguments that once the sequence starts cycling all possible numbers have been visited:

> lm(2)
[1] 1
> lm(3)
[1] 0


While I cannot guess the pattern, there seems to be much larger cases when lm(N) is equal to one, as for instance 173, 174, 173, 473, 774 (and plenty in-between).

## 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


## riddle of the seats

Posted in Statistics with tags , , , on November 8, 2019 by xi'an

An arithmetic quick riddle from The Riddler:

If an integer n is a multiple of every integer between 1 and 200, except for two consecutive ones, find those consecutive integers.

Since the highest power of 2 less than 200 is 2⁷=128 and since 127 is a prime number, the number

$2^6\times \prod_{i=0,i\ne 63}^{99} (2i+1)$

should work in that it contains all odd integers but 127, and all even numbers, but 128. Of course a smaller number that avoids duplicates by only considering the 44 primes other than 127 and 2 to a power that keep them less than 200 is also valid. Which gives a number of the order of 1.037443 10⁸⁵.

## another attempt at code golf

Posted in Books, Kids, R with tags , , , on June 12, 2019 by xi'an

I had another lazy weekend go at code golf, trying to code in the most condensed way the following task. Provided with a square matrix A of positive integers, keep iterating the steps

• take the highest square $$𝑥²$$ in A.
• find the smallest adjacent neighbour $$𝑛$$
• replace with x and n with nx

until no square is left (with neighbour defined as either horizontally or vertically and without wrapping around). While I managed a 217 bytes solution, compared with Robin’s 179b improvement, which remains surprising readable!, the puzzle offers two further questions:

1. is there a non-iterative way to find the final matrix B?
2. the puzzle assumes that A satisfies that at each step, the highest square and the smallest neighbour n will be unique, and that the sequence will not repeat forever. Is there a fool-proof way to check this is the case?

## an attempt at code golf

Posted in Kids, R with tags , , , , , , , , , on May 15, 2019 by xi'an

Having discovered codegolf on Stack Exchange a few weeks ago, I spotted a few interesting puzzles since then but only got the opportunity at a try over a quiet and rainy weekend (and Robin being on vacation)! The challenge was to write an R code for deciding whether or not a given integer n is congruent or not, when congruent means that it is the surface of a rectangle triangle with all three sides rational. The question included a pointer to the Birch and Swinnerton-Dyer conjecture as a mean to check congruence although the real solution was provided by Tunnell’s Theorem, which states that n is congruent if and only if the number of integer solutions to 2x²+y²+8z²=n is twice as much as the number of integer solutions to 2x²+y²+32z²=n if n is odd and  the number of integer solutions to 8x²+y²+16z²=n is twice as much as the number of integer solutions to 8x²+y²+64z²=n if n is even. Although this is only true for squared-free integers. (I actually spent more time on figuring out the exact wording of the theorem than on optimising the R code!)

My original solution

p=function(n){
for (i in(n:2)^2)if(n%%i<1)n=n/i
if(n%%2){d=8;f=2;g=16}else{d=2;f=1;g=8}
A=0;b=(-n:n)^2
for(x in d*b)for(y in x+f*b)for(z in g*b)
A=A+(y+z==n)-2*(y+4*z==n)
A==0}


was quite naïve, as shown by the subsequent improvements by senior players, like the final (?) version of Guiseppe:

function(n){b=(-n:n)^2
for(i in b[b>0])n=n/i^(!n%%i)
P=2^(n%%2)
o=outer
!sum(!o(y<-o(8/P*b,2*b,"+")/P-n,z<-16/P*b,"+"),-2*!o(y,4*z,"+"))}


exhibiting a load of code golf tricks, from using an anonymous function to renaming functions with a single letter, to switching from integers to booleans and back with the exclamation mark.