The random variable is uniformly distributed on the segment . For n > 0, its probability density is constant on this segment and equal to zero outside it:
The payoff W = X^2 is a function of the random variable . Therefore, its expected value is computed as the integral of x^2 multiplied by the density over the entire range of possible values of :
As a check, the result can be obtained another way, using the variance. For the uniform distribution on , it is known that E[X] = n / 2 and Var[X] = n^2 / 12. Then
Both approaches give the same answer.
Thus, the answer is computed by the formula n^2 / 3 in time.
Example
For n = 10 the answer is
Algorithm implementation
Read the input value .
scanf("%lld", &n);Compute and print the answer.
res = n * n / 3.0;
printf("%lf\n", res);