You start with start dollars and bet $1 a round, winning with probability p, independently. You stop at target dollars (success) or 0 (ruin).
Write gamblers_ruin(start: int, target: int, p: Fraction) -> Fraction: the exact probability of reaching target first.
gamblers_ruin(3, 10, Fraction(1, 2)) # Fraction(3, 10)
gamblers_ruin(1, 2, Fraction(2, 3)) # Fraction(2, 3) one round decides it
gamblers_ruin(0, 5, Fraction(1, 3)) # Fraction(0, 1) already ruined
gamblers_ruin(5, 5, Fraction(1, 3)) # Fraction(1, 1) already there
1 <= target <= 1000, 0 <= start <= target, 0 < p < 1. With P[i] the success probability from i dollars: P[0] = 0, P[target] = 1, P[i] = p·P[i+1] + q·P[i-1] where q = 1 - p. Simulation isn't exact.
⭐ Bonus: solving the system with general elimination (O(n³) Fraction work) passes for small targets; a speed test with targets up to 1000 earns a star with a closed form.
Show hint
the differences P[i+1] - P[i] form a geometric sequence with ratio r = q / p; sum it, and watch out for r = 1.