**I** found this *Le Monde* puzzle of June 16 I had stored and then somehow forgotten with my trips to Japan and Australia: *There are n beans in a box, with 98≤n≤102). Two players take at each round either one bean from the box or “the little half” (i.e. the integral part of the half) of the remaining beans. The player remaining with a single bean to pick has lost. What is the value of n for which there exists no winning strategy for the second player?* Now the R resolution is rather easy: Player 1 wins with *n* beans if Player 2 loses with either *(n-1)* or *[n/2]* beans. This leads to a straightforward recursive function

solve=function(n){
if (n<4){
solve=(n>1)}else{
solve=(!(solve(n-1)))&&(!solve(trunc(n/2)))}
solve}

as it is possible to win with 2 or 3 beans, providing the answer to the puzzle:

> solve(98)
[1] TRUE
> solve(99)
[1] FALSE
> solve(100)
[1] FALSE
> solve(101)
[1] FALSE
> solve(102)
[1] TRUE

if not an explanation!

Filed under: Kids, R Tagged: Le Monde, mathematical puzzle, R, recursive function

*Related*

To

**leave a comment** for the author, please follow the link and comment on his blog:

** Xi'an's Og » R**.

R-bloggers.com offers

**daily e-mail updates** about

R news and

tutorials on topics such as: visualization (

ggplot2,

Boxplots,

maps,

animation), programming (

RStudio,

Sweave,

LaTeX,

SQL,

Eclipse,

git,

hadoop,

Web Scraping) statistics (

regression,

PCA,

time series,

trading) and more...

If you got this far, why not

__subscribe for updates__ from the site? Choose your flavor:

e-mail,

twitter,

RSS, or

facebook...

**Tags:** Kids, Le Monde, mathematical puzzle, R, recursive function