Archive for arithmetics

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

 for (i in(n:2)^2)if(n%%i<1)n=n/i
 for(x in d*b)for(y in x+f*b)for(z in g*b)

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

for(i in b[b>0])n=n/i^(!n%%i)

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.

Le Monde puzzle [#1083]

Posted in Books, Kids, R, Travel with tags , , , , , , on February 7, 2019 by xi'an

A Le Monde mathematical puzzle that seems hard to solve without the backup of a computer (and just simple enough to code on a flight to Montpellier):

Given the number N=2,019, find a decomposition of N as a sum of non-trivial powers of integers such that (a) the number of integers in the sum is maximal or (b) all powers are equal to 4.  Is it possible to write N as a sum of two powers?

It is straightforward to identify all possible terms in these sums by listing all powers of integers less than N

for (pow in 3:11)

which leads to 57 distinct powers. Sampling at random from this collection at random produces a sum of 21 perfect powers:


But looking at the 22 smallest numbers in the pool of powers leads to 2019, which is a sure answer. Restricting the terms to powers of 4 leads to the sequence

1⁴+2⁴+3⁴+5⁴+6⁴ = 2019

And starting from the pools of all possible powers in a decomposition of 2019 as the sum of two powers shows this is impossible.

Le Monde puzzle [#1075]

Posted in Books, Kids, R with tags , , , , on December 12, 2018 by xi'an

A new Le Monde mathematical puzzle in the digit category:

Find the largest number such that each of its internal digits is strictly less than the average of its two neighbours. Same question when all digits differ.

For instance, n=96433469 is such a number. When trying pure brute force (with the usual integer2digits function!)

while (length(solz)>0){
 for (i in (10^(le+1)-1):(9*10^le+9)){
  x=as.numeric(strsplit(as.character(i), "")[[1]])
 if (min(x[-c(1,le+1)]<(x[-c(1,2)]+x[-c(le,le+1)])/2)==1){ print(i);solz=c(solz,i); break()}}

this is actually the largest number returned by the R code. There is no solution with 9 digits. Adding an extra condition

while (length(solz)>0){
 for (i in (10^(le+1)-1):(9*10^le+9)){
  x=as.numeric(strsplit(as.character(i), "")[[1]])
 if ((min(x[-c(1,le+1)]<(x[-c(1,2)]+x[-c(le,le+1)])/2)==1)&
    (length(unique(x))==le+1)){ print(i);solz=c(solz,i); break()}}

produces n=9520148 (seven digits) as the largest possible integer.