# the little half (another Le Monde puzzle)

October 27, 2012
By

(This article was originally published at Xi'an's Og » R, and syndicated at StatsBlogs.)

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 whatever his strategy 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!

Update (5:40, Central Time Zone): Robin’s question made me realise I had changed the wording of the problem when trying to solve it, moving from the little half to the large half in the above code! This means I should have used ceiling instead of trunc. The corrected R code stands as follows:

```solve=function(n){
if (n<3){
solve=(n==2)}else{
solve=(!(solve(n-1)))&&(!solve(ceiling(n/2)))}
solve}
```

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

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

Please comment on the article here: Xi'an's Og » R

Tags: , , , , ,

 Tweet

Email: