function d = Apportion(p,n,Method)
% p is an m-by-1 array of state populations
% n is the number of Congressional seats to apportion
% Method is a string that indicates the method of apportionment:
% SD smallest divisors
% LD largest divisors
% MF major fractions
% EP equal proportions
% HM harmonic mean
% d is an m-by-1 array with d(i) the number of seats apportioned to state
% i.

m = length(p);
d = ones(m,1);
for k=m+1:n
% Allocate the k-th seat...
f1 = p./d;
f2 = p./(d+1);
if strcmp(Method,'SD')
f = f1;
elseif strcmp(Method,'LD')
f = f2;
elseif strcmp(Method,'MF')
f = (f1 + f2)/2;
elseif strcmp(Method,'EP')
f = sqrt(f1.*f2);
else
f = 2./(1./f1 + 1./f2);
end
[mx,imx] = max(f);
imx= min(imx); % If multiple components in f have the same max value
% mx, then imx would be a vector. Using min here is
% just one option of tie-breaking in order to select
% only one state.
d(imx) = d(imx)+1;
end