% Simple MonteCarlo calculation of the volume of the unit hypersphere
% in Ndim dimensions.
function volume = hypersphere1(Ndim, Npoints)

Ndim = round(Ndim);
if (Ndim < 1) 
   error('Ndim must be 1 or more.');
end

x = rand(Ndim, Npoints);

r2 = sum(x.^2, 1); % sums columns of x.^2 only

Ninside = sum(r2<1, 2); % sums along the row

volume = (Ninside/Npoints)*2^Ndim;
   % 2^Ndim is the number of quadrants, octants, or whatever, 
   % in the hypercube. Each of these has volume 1^Ndim = 1.