◆ THE BLUEPRINT
The z-test is an approximation
$$z = \frac{\hat{p} - p}{\mathrm{SE}} \qquad \mathrm{SE} = \sqrt{\frac{p(1-p)}{n}}$$
where \(p\) is the null value and \(\hat{p}\) is the proportion we observed. The p-value is a normal tail area standing in for a binomial one. The stand-in holds when \(np \ge 10\) and \(n(1-p) \ge 10\); below that the binomial is discrete, floored at zero, and skewed, and the curve's thin tail undercounts extreme outcomes.
The exact answer needs no curve
Under H\(_0\), the count of events in \(n\) independent trials is Binomial\((n, p)\), so the honest p-value is a finite sum: $$P(X \ge x) = \sum_{k=x}^{n} \binom{n}{k} p^k (1-p)^{n-k}$$ The marble cup performs this distribution physically; replicate() performs it ten thousand times; pbinom() sums it exactly. All three agree because they are the same computation at different sizes.