Work out how precise a sample mean is, and how big a sample a given precision needs.
What to build
A program that prints a standard error and a required sample size.
Requirements
- Print
standard error S(sigma ÷ √n, 3 decimal places) andsample size N, the smallest n whose standard error is at most the target.
Input
One line: sigma, n and the target error.
Output
Two lines.
Constraints
sigma and the target are more than 0; 1 ≤ n ≤ 10⁶.
Example
Input:
15 25 1
Output:
standard error 3.000
sample size 225
Why it works
The standard error shrinks with the square root of the sample size, so the size needed for a target grows with the square of sigma ÷ target.
Examples
Example 1 · Input
15 25 1
Expected output
standard error 3.000 sample size 225
Example 2 · Input
10 100 0.5
Expected output
standard error 1.000 sample size 400
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The standard error is sigma ÷ √n.
Hint 2
The sample size for a target error is the smallest whole number at least (sigma ÷ target)²; subtract 0.000000001 before rounding up to absorb rounding errors.
Solution
Show one correct solution (try it yourself first)
from math import ceil
sigma, n, target = map(float, input().split())
print(f"standard error {sigma / n ** 0.5:.3f}")
print("sample size", ceil((sigma / target) ** 2 - 1e-9))