function np = countprimes_p(n)
% Returns the number of prime numbers below n.

np = 0;

parfor i = 2:n
p = 1; % if i is prime, p will be 1
    for j = 2:(i-1) % what to use as upper bound, instead of (i-1)?
        if mod(i, j) == 0
            p = 0;
        end
    end
    
    % The vectorized version:
    % However, for large values of i, this requires a long vector,
    % 2:(i-1), which is not practical.
    %m = mod(i, 2:(i-1));  % vectorize the loop above
    %p = 1 - any(m == 0);  % if any element of m is 0, then i is not prime
    %                      % and p becomes 0
    %
    np = np + p;
end

return
