## Susan has a prime frog.

Her frog is jumping around over $500$ squares numbered $1$ to $500$.

He can only jump one square to the left or to the right, with equal probability, and he cannot jump outside the range $[1;500]$.(if it lands at either end, it automatically jumps to the only available square on the next move.)

When he is on a square with a prime number on it, he croaks ‘P’ (PRIME) with probability $2/3$ or ‘N’ (NOT PRIME) with probability $1/3$ just before jumping to the next square.

When he is on a square with a number on it that is not a prime he croaks ‘P’ with probability $1/3$ or ‘N’ with probability $2/3$ just before jumping to the next square.

Given that the frog’s starting position is random with the same probability for every square, and given that she listens to his first $15$ croaks, what is the probability that she hears the sequence PPPPNNPPPNPPNPN?

Give your answer as a fraction $p/q$ in reduced form.

### This problem can be solved using a combination of prime number theory and Markov chain analysis.

First, let’s make an approximation that the density of primes around a large number `n` is roughly `1/log(n)`. Since we’re dealing with numbers from 1 to 500 here, we can safely take log to base 10, so the density of prime numbers in this range is approximately `1/log(500) = 1/2.7 = 0.37`. So, the probability that a number is prime is `0.37` and not prime is `1-0.37 = 0.63`.

Second, let’s denote `P` as `1` and `N` as `-1`. Then, the sequence `PPPPNNPPPNPPNPN` becomes a sequence of `+1, +1, +1, +1, -1, -1, +1, +1, +1, -1, +1, +1, -1, +1, -1`.

Now, consider a 2-dimensional grid where the horizontal axis represents time (from 0 to 15) and the vertical axis represents the sum of the sequence (ranging from -15 to +15). The starting point is at `(0, 0)` at time `t=0`.

The frog can move one step vertically upwards (with a probability of `2/3` if current position is prime or `1/3` if it’s not prime) or downwards (with `1/3` if current position is prime or `2/3` if it’s not prime). However, it cannot move beyond the upper limit of `+15` or below `-15`. This movement of the frog can be modeled by a Markov chain.

Next you calculate the probability to reach each point `(t, s)` on the grid (where `t` is the time and `s` is the sum of frog croak sequence up to time `t`) starting from `(0, 0)`.

You can do this using a dynamic programming approach setting `P(0, 0) = 1` and for each subsequent `t` and `s` calculate `P(t, s)` as the sum of probabilities to get to the adjacent points on grid from the previous time step, i.e., `P(t, s) = P(t-1, s-1)*upwards transition probability + P(t-1, s+1)*downwards transition probability`.

Upwards and downwards transition probabilities are calculated as explained earlier and depend on the primes density and current point whether it is prime or not.

Finally, you will have to calculate the probability of reaching the final sum of +3 (`PPPPNNPPPNPPNPN` has +3 as its sum) at time `t=15`, i.e., `P(15, 3)`.

This approach provides a huge performance improvement over checking every single sequence of 15 movements, which would require computing 2^15 = 32768 sequences.

Please note this is an approximate solution and due to approximation of the density of primes and handling edge cases of the grid (where frog is at an end and has only one direction to move to), the final result might not be 100% accurate.

##### More Answers:

Modulo SummationsRooms of Doom

Lowest-cost Search