You have three coins in your pocket…

R data science The Riddler

Solving a puzzle from 538’s Riddler Express.


Author

Affiliation

Miha Gazvoda

 

Published

March 6, 2021

DOI


This weekend I’ve been solving the March 5 538’s Riddler Express:

You have three coins in your pocket, each of which can be a penny, nickel, dime or quarter with equal probability. You might have three different coins, three of the same coin or two coins that are the same and one that is different.

Each of these coins can buy you a string whose length in centimeters equals the value of the coin in cents, i.e., the penny buys 1 cm of string, the nickel buys 5 cm of string, etc. After purchasing your three lengths of string, what is the probability that they can be the side lengths of a triangle?

Although the analytical solution is simple, I chose the simulation. More fun!

Solution

The possible coin values, i.e. lengths of strings we can buy with the coins:L indicates integer.

coin_sample_space <- c(1L, 5L, 10L, 25L)

With the sample function, we sample with replacement three coins that will be in our pocket.

sample(coin_sample_space, size = 3, replace = TRUE)
[1] 10  5  5

With these three coins, we buy strings of appropriate lengths. To determine whether the strings can shape a triangle, the sum of the shorter two has to be longer than the longest one.

is_triangle <- function(coins) {
  sum(coins) > 2 * max(coins)
}

Note that sum(coins) already contains maximum length - that’s why we are comparing it to two times the max length.

We repeat the same process zillion, okay n, times:

  1. Sample three coins with replacement from coin_sample_space.
  2. Determine whether we can shape a triangle with the purchased strings.
n <- 1e5

are_triangles <- replicate(
  n, 
  is_triangle(sample(coin_sample_space, size = 3, replace = TRUE))
)

mean(are_triangles)
[1] 0.34061

In around 34% of cases, we can shape the triangle. In the rest, we will have to live without it.

Footnotes

  1. L indicates integer.[↩]