Archive for The Riddler

simulated annealing and logistic regression to the max

Posted in Kids, R, Statistics with tags , , , , , , on April 26, 2023 by xi'an

A Riddler puzzle on the three binary and sequential questions one should ask three players hiding their respective U(0,1) realisation, U, V, and W, to best guess which player holds the largest number, max{U,V,W}. Assuming questions of the type Is U<b¹, &tc., the challenge boils down to selecting seven bounds (one for U, two for V, and four for W) in order to optimise the probability of picking the right player. Which means for a given collection of such bounds to learn this probability from the three binary answers. These can be turned into eight (2³) binary variables and I used them as entries in a logistic regression to predict that W was larger than max(U,V), itself predicted by the first two answers. The optimisation of the bounds can then be achieved by simulated annealing (or otherwise) and the approach returns (random) outputs like the following bounds (one on U, two on V, and four on W)

0.616 0.434 0.830 0.350 0.736 0.913 0.796 0.827

for an estimated probability of 0.827. This is a somewhat coherent sequence of bounds when considering the simpler case of two players. Indeed, with three bounds, the probability of winning can be readily derived as

b^2(b^1-b^2)+1/2+b^3(1-b^2+b^1)-b^1b^1

logically optimised by (b¹,b²,b³)=(1/2,1/4,3/4) for a success probability of 0.875. And it coïncides with the solution posted by The Riddler, although there is no intuition behind the figures, contrary to the two player situation. In fact, I am surprised that the bound on W does not equate the expectation of max{U,V} under the current conditions:

> x=runif(1e6,0,.616);y=runif(1e6,0,.434)
> mean(y+(x-y>0)*(x-y))
[1] 0.3591648

common leftovers

Posted in pictures, Travel with tags , , , , on April 5, 2023 by xi'an

The Riddler was about a number game, which meant starting a sum at 0 and adding i to the current sum at time i, almost a Fibonacci sequence but not exactly, since the sequence is simply

u_i=\dfrac{i(i+1)}{2}

which is easy to code, is of period 200, and this code showed that 3, 28, 53, and 78 were the most frequent last two digits. Or all 3 modulo 25 digits.

alone in Napoli

Posted in Books, Kids, R, Statistics with tags , , , , , , , , on March 13, 2023 by xi'an


A combinatorics puzzle from The Riddler about a Napoli solitaire where 4 x 10 cards numbered from 1 to 10 are shuffled and the game is lost when a number (1,2, or 3) is equal to its position modulo 3 (1,2 or 3). A simple R code shows that the probability of winning is around 0.00831:

N=40
for(t in 1:1e6)F=F+!sum(!(sample((1:N)%%10)-(1:N)%%3))

ChatGPT bends over backward to achieve this figure! Now, the exact probability can be found by combinatorics. While there are 40! ways of permuting the 40 cards, those missing the coincidences are

\sum\limits_{k=0}^4\sum\limits_{j=0}^4\sum\limits_{i=0}^4{13\choose i}{13\choose 4-i}{14\choose j}{13-i\choose 4-j}{14-j\choose k}{9+i\choose 4-k}

multiplied by 4!4!4!28! (which I initially forgot), resulting in 0.00831:

for(i in 0:4)for(j in 0:4)for(k in 0:4)
      F=F+exp(lchoose(13,i)+lchoose(13,4-i)+3*lfactorial(4)+
          lchoose(14,j)+lchoose(13-i,4-j)+lfactorial(28)+
          lchoose(14-j,k)+lchoose(9+i,4-k)-lfactorial(40))

Diophantine riddle

Posted in Books, Kids, R with tags , , , , , , , on February 27, 2023 by xi'an

The weekly riddle from The Riddler is to find solutions to the Diophantine equation

c³-c=b²+4

(when b and c are positive integers). First, forget about ChatGPT since it states this is a Pell equation. With a wrong argument. Second, when running a basic R code, using as.double to handle larger integers, the only solution less than 10⁶ this code returned was

[1]    999799 999700015

with the first column being c and the second b. But this is not a correct solution!, as confirmed by Mathematica, which states there is no integer solution. This makes sense when looking at the unique real solution (in c) of the cubic

c³-c-(b²+4)=0

since the solution (using Cardano’s formula) involves

\sqrt[3]{\frac{b^2+4}{2}\pm\sqrt{\frac{(b^2+4)^2}{4}-\frac{1}{27}}}

leaving the inverse of 27 as the only non-integer term in the expression when b is even… (The exact solution that this Diophantine equation has no solution is simpler: the lhs is a multiple of 3, while the rhs cannot be, as shown by looking at b(3).)

random walk with renewal riddle

Posted in Books, Kids, Statistics with tags , , , on February 19, 2023 by xi'an

The Riddler of this week had a rather straightforward puzzle, which can be summarised as finding the expectation of the number of steps needed for a random walk on {1,2,…,N} to reach state 1 when starting at N, if it moves by -1 with probability p and back to N with probability (1-p). Since the return to N is a renewal, the number of steps M is equal to N-1 with probability pN-1 and to i+1+M’ with probability pi(1-p) for 0≤i≤N-2, M and M’ being iid. Hence a point fixe equation

E=(N-1)p^{N-1}+\sum_{i=0}^{N-2}(i+1+E)p^i(1-p))

leading to

E=\frac{1-p^{N-1}}{p^{N-1}(1-p)}

which correctly returns p⁻¹ when N=2.