Premium problem49. Gambler's Ruin

Medium Locked

You begin with start units. Each round you win one unit with probability p and lose one otherwise, stopping at either goal or 0. Return the probability of reaching goal first.

With q=1−pq = 1 - p and r=q/pr = q/p:

P={startgoalp=0.51−rstart1−rgoalp≠0.5P = \begin{cases} \dfrac{\text{start}}{\text{goal}} & p = 0.5 \\[6pt] \dfrac{1 - r^{\text{start}}}{1 - r^{\text{goal}}} & p \neq 0.5 \end{cases}

Input

start = 10
goal = 20
p = 0.5

Output

0.5

Premium problem

This one's part of Premium. Unlock the full Probability track plus every other premium problem on the site.

Implement solve(...)