TITLE: calculating Möbius function
QUESTION [16 upvotes]: I wonder if there is any efficient way to calculate Möbius function for a array of number 1:1000000 
http://en.wikipedia.org/wiki/M%C3%B6bius_function

REPLY [3 votes]: I also like thisone, $\mathcal{O}(N \ln N)$ additions being not so different to $\mathcal{O}(N \ln \ln N)$ multiplications
% N(1+ln N) additions/affectations. an array of 2 bits elements would be enough

% matlab 
function mu = computeMu(N)
    mu = zeros(1,N); mu(1) = 1; 
    % Will compute the Dirichlet inverse of any sequence starting with 1
    for n = 1:N
        mu(n+n:n:end) = mu(n+n:n:end) - mu(n);
    end
end