the little half (another Le Monde puzzle)

[This article was first published on Xi'an's Og » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

To leave a comment for the author, please follow the link and comment on their blog: Xi'an's Og » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)