function n=binom2samn(p1,p2,alpha);
% format n=binom2sampn(p1,p2,alpha);
% where n is the desired sample size for each proportion in a 2-sample binomial test
%       p1 1st proportion, p2 second proportion
%       alpha=alpha level for test (optional, 0.05 if left out);
% based on a 2/11/05 sci.stat.edu post by Ray Koopman
% written by e gallahger 2/11/05
if nargin<3
    alpha=0.05;
end
z=erfinv(1-alpha)*sqrt(2);
p=[p1 p2]'; 
if p1<p2
    flipud(p);
end
n=z.^2* sum(p) *(2 - sum(p)) / (2 * diff(p).^2)