Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Here is the mathematical puzzle of the weekend edition of Le Monde:
Consider a sequence where the initial number is between 1 and 10³, and each term in the sequence is derived from the previous term as follows:
- if the last digit of the previous term is between 6 and 9, multiply it by 9;
- if the last digit of the previous term is between 0 and 5, take this digit away
Show that all sequences stop and give the length of the largest sequence.
To solve it, I wrote the following code:
seqlength=function(i){ las=i %% 10 #final digit if (las>5){u=i*9}else{u=trunc(i/10)} if(u==0){return(1)}else{return(seqlength(u)+1)} }
and found that the largest sequence started with 119 and lasted 16 steps:
[1] 119 [1] 1071 [1] 107 [1] 963 [1] 96 [1] 864 [1] 86 [1] 774 [1] 77 [1] 693 [1] 69 [1] 621 [1] 62 [1] 6 [1] 54 [1] 5
Similarly, the optimum between 1 and 10⁵ is attained for 95209 with 41 steps..
The math solution for the sequence to converge is a two-line proof as the transition from x to x1 either sees the removal of the last digit or a multiplication by 9, and the transition from x1 to x2 sees at worst a multiplication by 10 in the first case and always an integer division by 10 in the second case (since 9×6=54,…). So the move from x to x2 is always strictly decreasing unless x or x1 is zero, in which case the sequence ends up. Therefore, a strictly decreasing integer sequence can only stop in zero.
Filed under: R Tagged: Le Monde, mathematical puzzle, 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.