Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I’m history! No, I’m mythology! Nah, I don’t care what I am; I’m free hee! (Genie, when he is released from the magical oil lamp by Aladdin)
This is my own version of one of my favorite anti-common-sense mathematical curiosities. To explain it, let me start with an example. Imagine a simple chain with 7 links. If you open the 3rd link, the you split the chain into 3 pieces: a single link (the one you opened), a piece of 2 links and another one of 4 links. You could pay to Jasmine during seven days combining these 3 pieces:
- Day 1: Give her the single link
- Day 2: Give her the 2-links piece and take the single link, leaving her with 2 links
- Day 3: Give her the single link again, leaving her with 3 links
- Day 4: Give her the 4-links piece and take all pieces she has, leaving her with 4 links
- Day 5: Give her the single link again, leaving her with 5 links
- Day 6: Give her the 2-links piece and take 2-links piece, leaving her with 6 links
- Day 7: Give her the single link piece, leaving her with all links
Is easy to see that having a chain with 63 links, you could pay Jasmine breaking only 3 links (positions 5th, 14th and 31st). It easy to prove that the length of the biggest chain you can manage breaking only n links is (2n+1-1)*(n+1)+n
Next plot represents the minimum number of breaks to pay Jasmine daily for a given chain’s length. I call it the Jasmine’s Staircase:
Some curiosities around chains:
- Jasmine asked Jafar a chain of 66.571.993.087 links
- Supposing one link weights 4 grams, the chain of Jasmine would weight around 266 tons. It is supposed to be around 171 tons of gold in the world
- If you spend 1 second to climb the first step of the staircase, you will spend 302 years to climb the step number 100
Jafar was right. Jasmine was clever:
library(sqldf) library(ggplot2) library(extra) max.breaks=5 CalculateLength = function(n) {n+sum(sapply(0:n, function(x) 2^x*(n+1)))} results=data.frame(breaks=1:max.breaks, length=sapply(1:max.breaks, CalculateLength)) links=data.frame(links=2:CalculateLength(max.breaks)) results=sqldf("SELECT links.links, min(results.breaks) as minbreaks FROM links, results WHERE links.links <= results.length GROUP BY 1") opts=theme( panel.background = element_rect(fill="mistyrose"), panel.border = element_rect(colour="black", fill=NA), axis.line = element_line(size = 0.5, colour = "black"), axis.ticks = element_line(colour="black"), panel.grid = element_line(colour="white", linetype = 2), axis.text.y = element_text(colour="black"), axis.text.x = element_text(colour="black"), text = element_text(size=20, family="Humor Sans"), plot.title = element_text(size = 40) ) ggplot(results, aes(links,minbreaks))+ geom_area(fill="violet", alpha=.4)+ geom_step(color="violetred", lwd=1.5)+ labs(x="Chain's Length", y="Minimum Number of Breaks", title="Princess Jasmine's Staircase")+ scale_x_continuous(expand = c(0, 0), breaks = sapply(1:max.breaks, CalculateLength))+ opts
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.