Le Monde puzzle [#8]

[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.

Another mathematical puzzle from Le Monde that relates to a broken calculator (skipping the useless tale):

Given a pair of arbitrary positive integers (x,y) a calculator can either substract the same integer [lesser than min(x,y)] from both x and y or multiply either x or y by 2. Is it always possible to obtain equal entries by iterating calls to this calculator?

While the solution provided in this weekend edition of Le Monde is to keep multiplying x=min(x,y) by 2 until it is larger than or equal to y=max(x,y)/2,  at which stage subtracting 2x-y leads to (y-x,2y-2x) which is one multiplication away from equality, I wrote a simple R code that blindly searches for a path to equality, using as a target function exp{x²+y²+(x-y)²}. I did not even include a simulated annealing schedule as the optimal solution is known. Here is the R code:

#algorithm that brings two numbers (x,y) to be equal by
#operations x=2*x and (x,y)=(x,y)-(c,c)


 while (!stop){




For instance,

> emptied(39,31)
[1] 9 2
[1] 8 1
[1] 8 2
[1] 7 1
[1] 7 2
[1] 7 4
[1] 6 3
[1] 5 2
[1] 4 1
[1] 4 2
[1] 3 1
[1] 3 1
[1] 3 1
[1] 3 1
[1] 3 1
[1] 3 2
[1] 2 1
[1] 2 1
[1] 2 1
[1] 2 1
[1] 2 1
[1] 2 2

Filed under: R, Statistics Tagged: Le Monde, mathematical puzzle, R code

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)