# Probability of $k$ successes in no more than $n$ Bernoulli trials

I feel like these questions should be related to the binomial and geometric distributions, but I can't quite work out how.

Suppose I do sequential independent Bernoulli trials each with success probability $p$. Rather than fixing the number of the trials, as with the binomial distribution, I want to fix the number of successes I want to achieve. Call this number $k$. Once I obtain $k$ successes, I stop the Bernoulli trials. What is the probability that I perform exactly $n$ trials? What is the probability that I need to do no more than $n$ trials to achieve $k$ successes?

As a concrete example, suppose I'm flipping a fair coin and I will continue to do so until I see 20 heads. What is the probability that I see the 20th head on the 40th coin toss? What is the probability that I have to do 40 or fewer tosses until I see 20 heads?

Edit:

Ha, I don't know what it is about the answer coming to you as soon as you give up and ask for help, but I've got it now.

The probability of needing exactly $n$ trials for $k$ successes is just the binomial probability of $k−1$ successes in $n−1$ trials times the probability of success on the $n$th trial. The probability of needing no more than $n$ trials is the sum of the probabilities of needing exactly $k$ through $n$ trials. I'm pretty sure that works, let me know if you think this is wrong.

That said, if anyone has any ideas for turning this into a nicer, more compact expression, I'm all ears!

$\binom{20+20-1}{20} (\frac12)^{20} (\frac12)^{20} \approx 6\%$
This can be written as a regularized incomplete beta function as $1 - I_{1/2}(21, 20)$.