Ok, I have found an interesting probabilites problem on TopCoder. I have truncated the statement:
"What is the expected number of dice throws needed to attain a value of at least n (candies, in this problem)?"
So, the problem asks us to find the expected number of throws to obtain at least N candies. I'm not sure I get the meaning of 'expected value'. Thisi is some sort of average value needed to obtain at least N candies. So if I have K ways of obtaining at least N candies, and each way consist of a_i throws, then the expected value, would be:
E = ( a_1 + a_2 + ... + a_k )/k ?
But how do i find these ways? Because...as far as I'm cocerned, I would need to take into account an infinite number of values, because, if I need to obtain at least 10 candies, then obtaining 1000000 candies is indeed a valid method. So, could somebody give me a clearer picture of the concept of 'expected value', as it is used here, in this problem, and perhaps explain me why my reasoning is wrong?
Another thing...wouldn't it be use ful to know c[i][j], the probability of obtaining exactly i candies using j throws?
c[i][0] = 1, if i <= 0;
And then, if I know how to obtain, say, c[i-1][j-1] candies, then if I throw the dice now and I get a 1, I can obtain i candies. The probability of getting a 2 is 1/6. So, with a little reasoning and basic probabilty knowledge, we have:
c[i][j] = 1/6 ( c[i-1][j-1] + c[i-2][j-2] + c[i-3][j-3] + c[i-4][j-4] + c[i-5][j-5] + c[i-6][j-6] ), right ??
The answer of this problem is:
Res[n] = 1 + Res[n-1]/6 + Res[n-2]/6 + Res[n-3]/6 + Res[n-4]/6 + Res[n-5]/6 + Res[n-6]/6
Where Res[n] is the expected number of throws needed to obtain at least a value of n.
It is indeed clear for me that Res[n] = 1, and, for the purpose of easily implement this reccursion in a programming language, Res[n] = 0, n <= 0.
So, what eaxctly is meant here when 'the expected value' it's used?
Edit: Here are the examples the problem gives
n = 1, answer 1.0 n = 2, answer 1.1666666666666667 n = 7, answer 2.5216263717421126 n = 47, answer 13.90476189046144
For $N$ the expected value is the expected value until first seeing a number $\geq N$. For instance, for $N = 10$ and dice results $4,5,3,5,3,...$ the answer would be $3$, because $4+5 < 10,\text{ and } 4+5+3>10$, and for $5,6,3,2,1...$ it would be $2$. The bound on the expected value from above would be 10 (all 1's) and from above would be 2( you can't get 10 in fewer than two throws).
More formally, let $x_i$ be your dice results, and $S_n = x_1 + ... + x_n$. Then the expected number is $E(\tau)$, where $\tau = \min\{n|S_{n-1}<N, ~ S_n>=N\}$.
For example, for $N = 2$, $\tau$ is either 1 w.p. $\frac56$ (first die is in$\{2,...,6\}$) or it is $2$ w.p. $\frac16$ (first die is $1$, and then any other result brings you above $N=2$). So $E(\tau) = 1 \times \frac 56 + 2 \times \frac16 = 1.16161616$
For the second part of your question, here are two attempts to solve the problem itself, one for smaller numbers of $N$, the other for larger, and uses asymptotic.
For small numbers of $N$:
Denote $e_i$ the expected value of turns to get from $i$ to one of the states $\{N,N+1,...,N+5 \}$. Since we use a six-sided die, we will necessarily reach one of these, and will stop there.
Then we can write the following relations: $$e_0 = 1 + \frac 16 e_1 + \frac 16 e_2 + ... + \frac 16 e_6$$ $$e_1 = 1 + \frac 16 e_2 + \frac 16 e_3 + ... + \frac 16 e_7$$ $$...$$ $$e_{N-7} = 1 + \frac 16 e_{N-6} + \frac 16 e_{N-5} + ... + \frac 16 e_{N-1}$$ $$e_{N-6} = 1 + \frac 16 e_{N-5} + \frac 16 e_{N-4} + ... + \frac 16 e_{N-1} + \frac 16 0$$ $$e_{N-5} = 1 + \frac 16 e_{N-4} + \frac 16 e_{N-3} + ... + \frac 16 e_{N-1} + \frac 16 0 + \frac 16 0$$ $$...$$ $$e_{N-1} = 1$$
These is simply a system of linear equations that you can solve numerically.
An explanation on the (say) first equation. You start out at $0$. To get to the end set $\{N,...,N+5\}$ you need to make one step (that the $+1$ at the beginning), at which point w.p. 1/6 you end up on $1$, and will need further $e_1$ steps ("on average". Hence the expectation.) And so forth.
For large values of $N$:
The process $S_n = \sum_1^n x_i$, where $x_i$ are the results of the die, is a Markov Chain.
Our process ends at one of the states $\{N,...,N+5\}$.
For large values of $N$, we can use the theory of Markov Chains, and prove that the process will end on the state $N+i$ with probability $\pi_0 = \frac1{3.5}$ for $i = 0$, and $\pi_i = \frac{6-i}{6} \pi_0$. I am not presenting here the proof currently, to not overcomplicate things. If asked for, I could present it (though possibly it would be better to open another question for that.)
Then, we can invoke a version of the Wald's Equation. For a stopping time $\tau$ (which is well defined in our case, and is definitely finite), we write: $$E(S_{\tau}) = E(x)E(\tau)$$ ($S$ is the sum of $x_i$, $x$ is an individual die cast).
We wish to solve for $E(\tau) = \frac{E(S_{\tau})}{E(x)}$. $E(x)$ = $3.5$. From the probabilities above, $$E(S_{\tau}) = N \pi_0 + (N+1) \pi_1 + ... + (N+5) \pi_5 = ... = N + \frac53$$
So finally, we get the answer $E(\tau) = \frac{N+\frac53}{3.5}$. Is it good? Let's substitute $N = 47$ from the question. We get $13.9047619$. Hooray! It matches!
For $N = 7$ we get $2.4761$, which is not the number in your question, but is rather close, and is not bad for such a small value of $N$.