%mathpiper,def="Moebius"

// Algorithm adapted from:
// Elementary Number Theory, David M. Burton
// Definition 6.3 p120

5  # Moebius(1)	<-- 1;

10 # Moebius(_n)	<--
[
	Check(IsPositiveInteger(n), "Argument", "Moebius: argument must be positive integer");
        Local(factors,i,repeat);
	repeat:=0;
        factors:=Factors(n);
        len:=Length(factors);
        For(i:=1,i<=len,i++)[
		If(factors[i][2]>1,repeat:=1);
        ];
	If(repeat=0,(-1)^len,0);

];

%/mathpiper



%mathpiper_docs,name="Moebius",categories="User Functions;Number Theory"
*CMD Moebius --- the Moebius function
*STD 
*CALL
	Moebius(n)
*PARMS  

{n}     -- positive integer

*DESC

The Moebius function is 0 when a prime factor is repeated (which means it
is not square-free) and is $(-1)^r$ if $n$ has $r$ distinct factors. Also,
$Moebius(1)==1$.

*E.G.
In> Moebius(10)
Result: 1;
In> Moebius(11)
Result: -1;
In> Moebius(12)
Result: 0;
In> Moebius(13)
Result: -1;

*SEE DivisorsSum, ProperDivisors, ProperDivisorsSum, MoebiusDivisorsList
%/mathpiper_docs