the little half (another Le Monde puzzle)

October 27, 2012

(This article was first published on Xi'an's Og » R, and kindly contributed to R-bloggers)

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

if (n<4){

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

> solve(98)
[1] TRUE
> solve(99)
> solve(100)
> solve(101)
> 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 his blog: Xi'an's Og » R. 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: , , , ,

Comments are closed.