Before computing single probabilities, know where a binomial count is centred and how much it varies.
What to build
A program that prints the mean, variance and standard deviation of a binomial distribution.
Requirements
- Print
mean M,variance Vandsd S, each to 3 decimal places.
Input
One line: n (a whole number) and p (a decimal from 0 to 1).
Output
Three lines.
Constraints
1 ≤ n ≤ 1000000.
Example
Input:
10 0.5
Output:
mean 5.000
variance 2.500
sd 1.581
Why it works
Each trial adds p to the mean and p(1 − p) to the variance, and independent trials add up, which gives np and np(1 − p).
Examples
Example 1 · Input
10 0.5
Expected output
mean 5.000 variance 2.500 sd 1.581
Example 2 · Input
100 0.3
Expected output
mean 30.000 variance 21.000 sd 4.583
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Mean = n × p and variance = n × p × (1 − p).
Hint 2
The standard deviation is the square root of the variance.
Solution
Show one correct solution (try it yourself first)
n, p = input().split()
n, p = int(n), float(p)
variance = n * p * (1 - p)
print(f"mean {n * p:.3f}")
print(f"variance {variance:.3f}")
print(f"sd {variance ** 0.5:.3f}")