Calculating required sample size for an hypothesis test regarding a proportion Suppose we have the null and alternative hypotheses regarding a proportion
$H_0: p = p_0$
$H_a: p \ne p_0$
If we assume that $p$ takes the value $p_1$, what sample size do we need for a z-test of the null hypothesis at the $\alpha$ level with power $1 - \beta$?

The z-test based on the normal approximation rejects the null when the test statistic is greater than $z_{1 - \alpha/2}$,
$\big|{\frac{\hat{p} - p_0}{\sqrt{p_0 (1 - p_0) / n}}\big|} > z_{1-\alpha/2}$
Or, equivalently,
$\big|{\frac{\hat{p} - p_1}{\sqrt{p_1 (1 - p_1) / n}}\frac{\sqrt{p_1 (1 - p_1)}}{\sqrt{p_0 (1 - p_0)}} - \frac{p_1 - p_0}{\sqrt{p_0 (1 - p_0) / n}}\big|} > z_{1-\alpha/2}$
With $z = \frac{\hat{p} - p_1}{\sqrt{p_1 (1 - p_1) / n}}$, we have
$\big| z \frac{\sqrt{p_1 (1 - p_1)}}{\sqrt{p_0 (1 - p_0)}}  - \frac{p_1 - p_0}{\sqrt{p_0 (1 - p_0) / n}}\big| > z_{1-\alpha/2}$
or equivalently,
$\big| z   - \frac{p_1 - p_0}{\sqrt{p_1 (1 - p_1)/n}}\big| > z_{1-\alpha/2} \frac{\sqrt{p_0 (1 - p_0)}}{\sqrt{p_1 (1 - p_1)}}$
When $p = p_1$, $z \sim N(0,1)$ asymptotically, and the asymptotic power of the test is therefore equal to
$1 - \beta = \Phi\left(  \frac{\sqrt{n}|p_1 - p_0|  - z_{1-\alpha/2}\sqrt{p_0 (1 - p_0)}}{\sqrt{p_1 (1 - p_1)}}  \right) + \Phi\left(  \frac{-\sqrt{n}|p_1 - p_0|  - z_{1-\alpha/2}\sqrt{p_0 (1 - p_0)}}{\sqrt{p_1 (1 - p_1)}}  \right)$
Typically the second term on the right of the equation is quite small, and so we can approximate
$1 - \beta = \Phi\left(  \frac{\sqrt{n}|p_1 - p_0|  - z_{1-\alpha/2}\sqrt{p_0 (1 - p_0)}}{\sqrt{p_1 (1 - p_1)}}  \right)$
Taking the inverse of the normal on both sides,
$z_{1-\beta} =  \frac{\sqrt{n}|p_1 - p_0|  - z_{1-\alpha/2}\sqrt{p_0 (1 - p_0)}}{\sqrt{p_1 (1 - p_1)}} $
Solving for $n$,
$n = \left(\frac{z_{1-\beta}\sqrt{p_1 (1 - p_1)} +  z_{1-\alpha/2}\sqrt{p_0 (1 - p_0)}}{p_1 - p_0}\right)^2 $
