## Riddler Express Problem 4/10/2020

During a break at a music festival, the crew is launching T-shirts into the audience using a T-shirt cannon. And you’re in luck - your seat happens to be in the line of flight for one of the T-shirts! In other words, if the cannon is strong enough and the shirt is launched at the right angle, it will land in your arms.

The rows of seats in the audience are all on the same level (i.e., there is no incline), they are numbered 1, 2, 3, etc., and the T-shirts are being launched from directly in front of Row 1. Assume also that there is no air resistance (yes, I know, that’s a big assumption). You also happen to know quite a bit about the particular model of T-shirt cannon being used — with no air resistance, it can launch T-shirts to the very back of Row 100 in the audience, but no farther.

The crew member aiming in your direction is still figuring out the angle for the launch, which you figure will be a random angle between zero degrees (straight at the unfortunate person seated in Row 1) and 90 degrees (straight up). Which row should you be sitting in to maximize your chances of nabbing the T-shirt?

### Setup

The average value of the acceleration due to gravity, $$g$$, on Earth is about 9.8$$m/s^2$$ according to Universe Today. To simplify the equations the ensue, lets assume some other unit so the acceleration due to gravity is 1 (row length)/$$s^2$$.

Let’s also assume the cannon is at the origin of the Cartesian plane and that if a t-shirt is fired into region $$(r-1,r]$$, for some $$r=1,2,\ldots ,100$$ that the person sitting in row r will get the t-shirt.

We need to parametrize $$x$$ and $$y$$ with respect to $$t$$ (which we will assume is in seconds). Assume for the time being that the velocity at which t-shirts exit the cannon is $$v$$ row lengths per second. If the cannon is $$\theta$$ degrees above the horizontal, then it is not to hard to deduce the two differential equalities: $\frac{dx}{dt} = v\sin(\theta), \,\, \text{ and } \,\, \frac{dy}{dt} = v\cos(\theta)-t.$ Since $$t=0$$ is at the origin, we can use this initial condition to arrive at $x(t) = vt\sin(\theta) \,\, \text{ and } \,\, y(t) = vt\cos(\theta)-0.5t^2.$

Let’s go with our gut on this next part instead of trying to prove it, and say that the t-shirt will travel the furthest at a 45 degree angle from horizontal. Both cos and sin are $$1/\sqrt{2}$$ at that angle. Not knowing the velocity quite yet, let’s first solve for the time $$t^*$$ (in terms of the velocity) that the t-shirt hits the ground at 100. This equation

$y(t^*) = 0 = \frac{vt^*}{\sqrt{2}}-0.5(t^*)^2$ leads to $$t^* = \frac{v}{0.5\sqrt{2}}$$. When we plug this time into the horizontal function $$x(t)$$ we should get 100. That will help solve for $$v$$.

$x(t^*) = 100 = v^2$ which leads to $$v = 10$$.

Let’s build our horizontal and vertical functions in terms of the angle and the time in seconds.

## First the horizontal function x(theta, t)
horiz <- function(angle, t){
return(10*t*sin(angle*pi/180))
}

## and then the vertical function y(theta, t)
vert <- function(angle, t){
return(10*t*cos(angle*pi/180) - 0.5*t^2)
}

### Simulation

We will first set up a simulation to see which row has the largest probability of getting a t-shirt. In order to do this, we’ll choose a random angle between 0 and 90 degrees, and see where the t-shirt lands, tally that, and repeat a few thousand times.

Tshirt <- function(N){ # input N will be the number of simulations
rowseats <- rep(0, 100) ## Represents the number of t-shirts each seat has
for(i in 1:N){
angle <- runif(1,0,90)
# This would be the t for which y(t) = 0 at the other end
t <- 10*cos(angle*pi/180)/0.5
x <- ceiling(horiz(angle, t))
rowseats[x] <- rowseats[x]+1
}
return(rowseats)
}

set.seed(217)
Tshirt(1000000)
##     6293  6172  6321  6305  6322  6289  6198  6367  6304  6293  6548
##    6381  6299  6404  6438  6502  6465  6449  6535  6429  6431  6489
##    6622  6601  6674  6502  6738  6587  6646  6796  6687  6586  6743
##    6811  6729  6714  6830  6794  6932  7008  6874  6949  7000  7175
##    7104  7075  7267  7128  7302  7352  7433  7502  7366  7547  7667
##    7650  7829  7928  7901  7972  7897  8210  8214  8328  8297  8355
##    8452  8461  8783  8792  8793  8987  9093  9472  9514  9699 10034
##    9980 10349 10611 10758 11206 11426 11585 12049 12347 12689 13071
##   13615 14196 14955 15784 16640 18305 19399 21420 24292 29118 37466
##  90103

Row 100 looks like it should be the hot spot with about a 9% chance of getting a T-shirt! In order to solve for the exact probability, we need to find the values of $$\theta$$ such that $99 = x() = 200()(),$ and then divide the range of these values by 90 degrees. These angles are about 40.9452 and 49.0548, respectively. The range over 90 gives .09010667. Our simulated .090103 was not too far off.

# Riddler Classic

Over the course of three days, suppose the probability of any spammer making a new comment on this week’s Riddler column over a very short time interval is proportional to the length of that time interval. (For those in the know, I’m saying that spammers follow a Poisson process.) On average, the column gets one brand-new comment of spam per day that is not a reply to any previous comments. Each spam comment or reply also gets its own spam reply at an average rate of one per day.

For example, after three days, I might have four comments that were not replies to any previous comments, and each of them might have a few replies (and their replies might have replies, which might have further replies, etc.).

After the three days are up, how many total spam posts (comments plus replies) can I expect to have?

### Setup

In a poisson process, the time between occurences follows an exponential distribution. In this case with rate 1 per day. To simulate the amount of spammers the Riddler will get in a 3 day span, we should first recognize that a spammers comment, it doesn’t matter at what level the comment is made (first, second, third, etc.). WHat matters is that a whole new Poisson process is born.

Know this will aid in simplifying a simulation.

### Simulation

## Input M: number of simulations
## Output: median and mean number of spammers in 3 hours
simSpam <- function(M){
## the results from each simulation will be tracked
results <- c()
## the loop to simulate it M times
for(j in 1:M){
## i represents the current rate at which spammers comment
i <- 1
# N represents the number of spammers so far
N <- 0
# t represents the current time in hours
t <- 0
# while t is less than 3 hours, generate spam comments
while(t<3){
## next spam comment comes in with a time that is
## exponentially distributed with rate i
t <- t + rexp(1,i)
## if time less than 3 hours increment rate and
if(t<3){
i <- i+1
N <- N+1
}
}
results <- c(results, N)
}
## Finally, return the median and mean of the results.
return(c(Median = median(results), Mean = mean(results)))
}
set.seed(20200410)
simSpam(100000)
##   Median     Mean
## 13.00000 18.99752
simSpam(100000)
##   Median     Mean
## 13.00000 19.14026

### Exact mean

Through some inspiration from Josh Silverman (@_joshsilverman on Twitter), we can find the mean value exactly through the following reasoning using a differential equation.

If we think of $$S(t)$$ as the expected number of spam comments at time $$t$$, and know that the rate at which spam comments are being made is one greater than $$S(t)$$, the differential equation $\frac{dS}{dt} = S+1$ can be written. Solving this differential equation yields $$S(t) = e^t-1$$. Plugging in $$t=3$$ hours yields 19.0855369, which is very close to the simulated value.