TITLE: I roll a die repeatedly until I get 6, and then count the number of 3s I got. What's my expected number of 3s?
QUESTION [92 upvotes]: Consider the following experiment. I roll a die repeatedly until the die returns 6, then I count the number of times 3 appeared in the random variable $X$. What is $E[X]$?

Thoughts: I expect to roll the die 6 times before 6 appears (this part is geometric), and on the preceding 5 rolls each roll has a $1/5$ chance of returning a 3. Treating this as binomial, I therefore expect to count 3 once, so $E[X]=1$.
Problem: Don't know how to model this problem mathematically. Hints would be appreciated.

REPLY [4 votes]: Counting polynomials are fun.
A roll generates:
$$r = \frac{1}{6} x + \frac{1}{6} y + 4/6 r$$
or
$$r = \frac{1}{2} x + \frac{1}{2} y$$
where x is the number of 3s and y is the number of 6s.
$$1 + r + r^2 + r^3 + ...$$
$$=\frac{1}{1-r}$$
We then take the derivative with respect to $x$ to get its count-weighted value, then evaluate at $x=1$:
$$\frac{d}{dx}\frac{1}{1-(\frac{x}{2}+\frac{y}{2})} = \frac{2 (y'(x)+1)}{(x+y-2)^2}$$
Now we want to evaluate at $y=0$ (as any case with a non-zero number of 6s should be discarded):
For $y`(x)$:
$$y = 2r-x$$
$$y'(x) = r'(x) - 1$$
$$r'(x) = 1/2$$
$$y'(x) = 1/2-1 = -1/2$$
So we get:
$$\frac{2 (-\frac{1}{2}+1)}{(1+0-2)^2}$$
$$=\frac{1}{1}$$
Or, the number of 3s is 1 on average.
This strategy can be generalized for more complex situations.