Real questions ask for ranges: at most 2 faults, at least 8 correct answers. Add up the binomial probabilities and find the peak of the distribution.
What to build
A program that prints cumulative binomial probabilities and the mode.
Requirements
- Print
P(X<=k) AandP(X>=k) B, each to 6 decimal places, with k the given number. - Print
most likely M: the number of successes with the highest probability; if several share it (to 12 decimal places), the smallest of them.
Input
One line: n, k (whole numbers) and p (a decimal from 0 to 1).
Output
Three lines.
Constraints
0 ≤ k ≤ n ≤ 100.
Example
Input:
10 5 0.5
Output:
P(X<=k) 0.623047
P(X>=k) 0.623047
most likely 5
Why it works
Different numbers of successes are mutually exclusive, so their probabilities add for a range; the two ranges overlap at k itself, which is why they can add up to more than 1.
Examples
10 5 0.5
P(X<=k) 0.623047 P(X>=k) 0.623047 most likely 5
3 1 0.5
P(X<=k) 0.500000 P(X>=k) 0.875000 most likely 1
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
P(X ≤ k) adds the probabilities of 0 to k successes; P(X ≥ k) adds k to n.
Hint 2
For the most likely count, compare probabilities rounded to 12 decimal places so that exact ties are seen as ties.
Solution
Show one correct solution (try it yourself first)
from math import comb
n, k, p = input().split()
n, k, p = int(n), int(k), float(p)
probs = [comb(n, i) * p ** i * (1 - p) ** (n - i) for i in range(n + 1)]
print(f"P(X<=k) {sum(probs[: k + 1]):.6f}")
print(f"P(X>=k) {sum(probs[k:]):.6f}")
rounded = [round(q, 12) for q in probs]
print("most likely", rounded.index(max(rounded)))