The classical Möbius function: μ(n) is an important multiplicative function in number theory and combinatorics.

There are several ways to implement a Möbius function.

A fairly straightforward method is to find the prime factors of a positive integer n, then define μ(n) based on the sum of the primitive factors. It has the values {−1, 0, 1} depending on the factorization of n:

 μ(1) is defined to be 1.
 μ(n) = 1 if n is a square-free positive integer with an even number of prime factors.
 μ(n) = −1 if n is a square-free positive integer with an odd number of prime factors.
 μ(n) = 0 if n has a squared prime factor.


Task

 Write a routine (function, procedure, whatever) μ(n) to find the Möbius number for a positive integer n.
 Use that routine to find and display here, on this page, at least the first 99 terms in a grid layout. (Not just one long line or column of numbers.)


See also

 Wikipedia: Möbius function


Related Tasks

 Mertens function


func Mobius(N);
int  N, Cnt, F, K;
[Cnt:= 0;
F:= 2;  K:= 0;
repeat  if rem(N/F) = 0 then
                [Cnt:= Cnt+1;
                N:= N/F;
                K:= K+1;
                if K >= 2 then return 0;
                ]
        else    [F:= F+1;  K:= 0];
until   F > N;
return if Cnt&1 then -1 else 1;
];
 
int  N;
[Format(3, 0);
Text(0, "   ");
for N:= 1 to 199 do
        [RlOut(0, float(Mobius(N)));
        if rem(N/20) = 19 then CrLf(0);
        ];
]