[Question]
  [
[The Möbius function](https://en.wikipedia.org/wiki/M%C3%B6bius_function) is an important number theoretic function.
Your submission should accept a positive integer \$n\$ and return the value of the Möbius function evaluated at \$n\$.
### Definition
The Möbius function \$μ(n)\$ is defined as follows:
$$\mu(n) = \begin{cases}
\hphantom{-}1, &n\text{ is squarefree and has an even number of distinct prime factors} \\
-1, &n\text{ squarefree and has an odd number of distinct prime factors} \\
\hphantom{-}0, &\text{otherwise}
\end{cases}$$
\$n\$ is called *squarefree* if the exponents of the prime factorization of \$n\$ are all strictly less that two. (Alternatively: No prime to the power of two divides \$n\$).
### Test cases
Here you can see the first 50 values of μ:
![](https://upload.wikimedia.org/wikipedia/commons/c/c8/Moebius_mu.svg)
Public Domain Image from Wikipedia
The Möbius function is sequence number [A008683](https://oeis.org/A008683) in the OEIS.
These are the first 77 values:
```
1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0, -1, 1, 1, 0, -1, 0, -1, 0, 1, 1, -1, 0, 0, 1, 0, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1, 1, 1, 0, -1, -1, -1, 0, 0, 1, -1, 0, 0, 0, 1, 0, -1, 0, 1, 0, 1, 1, -1, 0, -1, 1, 0, 0, 1, -1, -1, 0, 1, -1, -1, 0, -1, 1, 0, 0, 1
```
Larger values can also easily be checked [in Wolframalpha.com](http://www.wolframalpha.com/input/?i=MoebiusMu+123) or in the [b-file of OEIS](https://oeis.org/A008683/b008683.txt), as suggested by @MartinBüttner.
  
[Answer]
## Python 2, 48 bytes
```
m=lambda n,d=1:d%n and-m(d,n%d<1)+m(n,d+1)or 1/n
```
Earlier 51 byte version:
```
m=lambda n:1/n-sum(m(k)for k in range(1,n)if n%k<1)
```
[Möbius-inverts](https://en.wikipedia.org/wiki/M%C3%B6bius_inversion_formula) the sequence `1,0,0,0,0...`.
The Möbius function has the property that for any `n>1`, the Möbius functions of `n`'s divisors sum to 0. So, for `n>1`, `μ(n)` is computed by negating the sum of `μ(k)` for all proper divisors `k` of `n`. For `n=1`, the output is `1`.
The code handles the base case by adding a floor-division `1/n`, which gives `1` for `n==1` and `0` otherwise.
Thanks to Dennis for saving 3 bytes with better recursive handling inspired by a similar structure in [this challenge](https://codegolf.stackexchange.com/a/83636/20260).
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
Code:
```
ÆF>1’PS
```
Explanation:
```
ÆF       # This computes the prime factorization as well as the exponent
  >1     # Compares each element if it's greater than 1, resulting in 1's and 0's
    ’    # Decrement on each element
     P   # Compute the product
      S  # Compute the sum of the list
```
For example, the number **10**:
```
ÆF       # [[2, 1], [5, 1]]
  >1     # [[1, 0], [1, 0]]
    ’    # [[0, -1], [0, -1]]
     P   # [0, 1]
      S  # 1
```
And results in **1**.
[Try it online](http://jelly.tryitonline.net/#code=w4ZGPjHigJlQUw&input=&args=MTA).
[Answer]
## Mathematica, 9 bytes
```
MoebiusMu
```
Of course, Mathematica has a built-in. (And ~~will probably be~~ is beaten by Jelly anyway.)
[Answer]
## CJam, ~~18~~ 15 bytes
```
WrimFz~\1&+f/:*
```
The fact that CJam returns 1 in factorisation builtins for `n = 1` makes things a bit tricky.
[Try it online](http://cjam.aditsu.net/#code=WrimFz~%5C1%26%2Bf%2F%3A*&input=1) | [Test suite](http://cjam.aditsu.net/#code=20%7B%29%0A%0AW%5CmFz~%5C1%26%2Bf%2F%3A*%0A%0A%7D%25p)
Thanks to @PeterTaylor for the neat `1&+` trick for handling the 1 case.
## Explanation
```
W                 Push -1
 ri               Push input as int
   mF             Factorise input into [base exponent] pairs
     z~           Zip and unwrap pairs, leaving stack like [-1 bases exponents]
       \1&        Setwise intersect bases with 1, giving [1] for 1 and [] otherwise
          +       Append to exponent array
           f/     Divide the previously pushed -1 by each element in the array 
                  This gives -1 for 1s and 0 otherwise, since / is int division
             :*   Take product
```
For `n > 1`, the modified array is just the exponents array. If `n` is squarefree then the array is all 1s, which become all -1s after division. Otherwise, if n has a prime square divisor, then there will be a 0 somewhere after division, giving a product of 0.
For `n = 1` the modified array is `[1] + [1]`, which becomes `[-1 -1]` after division, giving a product of 1.
---
Alternative 16:
```
rimF{1#2+3%(}%:*
```
This uses `#` (find) on each `[base exponent]` array to look for a 1, then maps `-1 -> 0, 0 -> 1, 1 -> -1`.
[Answer]
## Ruby, ~~65+7=72~~ 62+7=69 bytes
```
->x{((d=x.prime_division).all?{|_,n|n<2}?1:0)*(d.size%2*-2+1)}
```
+7 bytes for the `-rprime` flag.
We're doing this the very naïve way:
```
->x{
 (
  (d=x.prime_division)  # ex. input 20 results in [[2,2],[5,1]] here
  .all?{|_,n|n<2}?      # are all factors' exponents under 2?
  1:0                   # if so, result in a 1; otherwise, a 0
 )
 *                      # multiply that 1 or 0 by...
  (d.size%2*-2+1)       # magic
}
```
The "magic" part results in 1 if the number is even and -1 otherwise. Here's how:
```
Expression       Even  Odd
--------------------------
d.size%2         0     1
d.size%2*-2      0     -2
d.size%2*-2+1    1     -1
```
[Answer]
## [Labyrinth](https://github.com/mbuettner/labyrinth), 87 bytes
```
1
:
}
?   @ "}){/{=:
""({! "      ;
} )   :}}:={%"
* _}}){     ;
{      #}):{{
")`%#/{+
```
[Try it online!](http://labyrinth.tryitonline.net/#code=MQo6Cn0KPyAgIEAgIn0pey97PToKIiIoeyEgIiAgICAgIDsKfSApICAgOn19Oj17JSIKKiBffX0peyAgICAgOwp7ICAgICAgI30pOnt7CiIpYCUjL3sr&input=MTE)
### Short explanation
Here's a port of the algorithm used, in Python:
```
divisor = 1
mobius = 1
n = int(input())
while n != 1:
  divisor += 1
  count = 0
  while n % divisor == 0:
    n //= divisor
    count += 1
  mobius *= (count + 3)//(count + 1)%3*-1 + 1
print(mobius)
```
### Long explanation
The usual primer on Labyrinth:
* Labyrinth is stack-based and two-dimensional, with execution starting at the first recognised character. There are two stacks, a main stack and an auxiliary stack, but most operators only work on the main stack.
* At every branch of the labyrinth, the top of the stack is checked to determine where to go next. Negative is turn left, zero is straight ahead and positive is turn right.
Execution for this program starts at the top-left `1`.
```
Outer preparation
=================
1        Pop 0 (stack is bottomless and filled with 0s) and push 0*10+1 = 1
:}       Duplicate and shift to auxiliary stack
?        Read int from input
         Stack is now [div=1 n | mob=1]
Top of stack positive but can't turn right. Turn left into outer loop.
Begin outer loop
================
Inner preparation
-----------------
(        Decrement top of stack
If top was 1 (and is now zero), move forward and do...
------------------------------------------------------
{!       Print mob
@        Terminate
If top of stack was greater than 1, turn right and do...
--------------------------------------------------------
)        Increment n back to its previous value
_}       Push 0 and shift to aux
         This will count the number of times n can be divided by div
}){      Increment div
         Stack is now [div n | count mob]
Inner loop
----------
:}}      Dup n, shift both n's to aux
:=       Dup div and swap top of main with top of aux
{%       Shift div down and take mod
         Stack is now [div n%div | n count mob]
If n%div == 0, move forward and do...
-----------------------------------
;        Pop n%div
:={/     Turn n into n/div
{)}      Increment count
         (continue inner loop)
If n%div != 0, turn right (breaking out of inner loop) and do...
================================================================
;        Pop n%div
{{       Pull n and count from aux
:)}      Dup and increment count, giving (count+1), and shift to aux
#+       Add length of stack to count, giving (count+3)
{/       Calculate (count+3)/(count+1)
#%       Mod by length of stack, giving (count+3)/(count+1)%3
`        Multiply by -1
)        Increment, giving (count+3)/(count+1)%3*-1 + 1
         This is 1 if count was 0, -1 if count was 1 and 0 if count > 1
{*}      Multiply mob by this number
         (continue outer loop)
```
[Answer]
## Pyth, 9 bytes
```
^_{IPQlPQ
```
Explanation:
```
^_{IPQlPQ    Implicit: Q=input
    PQ       Prime factorization of Q
  {I         Is invariant under uniquify.
  {IPQ       1 if Q is squarefree; 0 otherwise.
 _{IPQ       -1 if Q is squarefree; 0 otherwise.
^     lPQ    Exponentiation to length of PQ.
```
Try it [here](https://pyth.herokuapp.com/?code=%5E_%7BIPQlPQ&input=14&test_suite=1&test_suite_input=1%0A5%0A10%0A15%0A20&debug=1).
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 8 bytes
Code:
```
.p0K1›<P
```
Explanation:
```
.p        # Get the prime factorization exponents
  0K      # Remove all zeroes from the list
    1›    # Compare each element if greater than 1
      <   # Decrement on each element
       P  # Take the product
```
Uses the CP-1252 encoding
[Answer]
# Java 8, ~~72~~ ~~68~~ ~~65~~ 64 bytes
```
n->{int r=1,i=1;for(;i++<n;)r=n%i<1?(n/=i)%i<1?0:-r:r;return r;}
```
-4 bytes thanks to *@PeterTaylor*.
Port of [@Meerkat's .NET C# answer](https://codegolf.stackexchange.com/a/174900/52210), which I first golfed a bit further, so make sure to upvote him!
[Try it online.](https://tio.run/##LY7PDoIwDMbvPEVDYrJlgnIwJo7pE@iFo/EwEUz9U0wZJobw7Lihly/t1/bX72bfNrld7mP5sG0Le4vURwBIruLalhUcQjsZUIqgJLV3hshL66zDEg5AYGCkZNuHBTbZHE2m64aFRqVy0pINzTDPdoIWBuVULjcJb1hz5TomYD2MOjBf3fnhmX/0u8ELPH0oUThGuh5PYOUvUcCHd@EVYL5eeVVKTjOA4tO66pk2nUtf/tAJSn18qeJ5/M8/jF8)
**Explanation:**
```
n->{                // Method with integer as both parameter and return-type
  int r=1,          //  Result-integer, starting at 1
  i=1;for(;i++<n;)  //  Loop `i` in the range [1, n]:
    r=n%i<1?        //   If `n` is divisible by `i`:
       (n/=i)       //    Divide `n` by `i` first
        %i<1?       //    And if `n` is still divisible by `i`:
         0          //     Change `r` to 0
        :           //    Else:
         -r         //     Swap the sign of `r` (positive to negative or vice-versa)
      :             //    Else:
       r;           //     Leave `r` unchanged
  return r;}        //  Return `r` as result
```
[Answer]
## CJam, 20 bytes
```
rimf1-___&=\,2%!2*(*
```
[Run it against a range of inputs here.](http://cjam.aditsu.net/#code=20%7B)%0A%0Amf1-___%26%3D%5C%2C2%25!2*(*%0A%0Ap%7D%2F)
Seems golfable.
[Answer]
# R ~~39~~16 bytes
```
numbers::moebius
```
Requires you having the [numbers](https://cran.r-project.org/web/packages/numbers/index.html) package installed on your system...
Edit: Far simpler if I read the specs appropriately [thanks @AlexA.]
[Answer]
# [Factor](https://factorcode.org) + `math.extras`, 6 bytes
```
mobius
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m70sLTG5JL9owU3H0GBPP3crhdzEkgy91IqSosRiCLsoMS89tVihOLWwNDUvGcgqKEotKaksKMrMK1Gw5jI3UIg21EmKVYheWlqSpmuxLDc_KbO0GMJZHgs0o0BBD8JbsABCAwA)
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 16 Bytes
```
?nl{PQlPQZ^_1lPQ
```
[Try it online!](http://pyth.herokuapp.com/?code=%3Fnl%7BPQlPQZ%5E_1lPQ&debug=0)
My first actual Pyth answer. Suggestions appreciated! :)
## Explanation
My solution uses the fact that a number is squarefree, if its prime-factors contain no number more than once. If the input is squarefree, the Möbius-Function takes the value -1^(number of primefactors).
```
?n        if not equal
  l{PQ      length of the list of the distinct input-Primefactors
  lPQ       length of the list of primefactors including duplicates    
    Z         Input is not squarefree, so output Zero
  ^_1lPQ  if input is squarefree, output -1^(number of prime-factors)
```
[Answer]
# [MATL](https://esolangs.org/wiki/MATL), 15 ~~17~~ bytes
```
tq?YftdAwn-1w^*
```
This uses [current release (10.1.0)](https://github.com/lmendo/MATL/releases/tag/10.1.0) of the language/compiler.
[**Try it online!**](http://matl.tryitonline.net/#code=dHE_WWZ0ZEF3bjJcLTIqUSo&input=MTA)
### Explanation
```
t         % implicit input. Duplicate that
q         % decrement by 1. Gives truthy (nonzero) if input exceeds 1
?         % if input exceeds 1, compute function. Otherwise leave 1 on the stack
  Yf      % vector of prime factors. Results are sorted and possibly repeated
  td      % duplicate and compute differences
  A       % true if all differences are nonzero
  w       % swap
  n       % number of elements in vector of prime factors, "x"
  -1w^    % -1^x: gives -1 if x odd, 1 if x even 
  *       % multiply by previously obtained true/false value, so non-square-free gives 0
          % implicitly end "if"
          % implicitly display stack contents
```
[Answer]
# [C# (.NET Core)](https://www.microsoft.com/net/core/platform), 86 73 72 65 bytes
```
a=>{int b=1,i=1;for(;++i<=a;)b=a%i<1?(a/=i)%i<1?0:-b:b;return b;}
```
[Try it online!](https://tio.run/##NYzLCsIwFET3/Yq7ERL60OpCbJqKCIKgGxVcJ2kqF2oCSVqQ0m@vRXBg4BwGRvlUWaenzqN5wf3jg34zUK3wHg4wRD6IgApOnVElmpDMraDhk@DVMDNInifIc9ZYR1gcY8kFo5KLBZb5noglR/rDVZHKQjKnQ@cMSDZO7P/dW6zhKtAQOkQAcLTG21ZnT4dBk4bM3msXsoc9m7BZk/9@06K@oNGEzmHROE677Rc "C# (.NET Core) – Try It Online")
*-13 bytes: streamlined loops, added return variable (thanks to [Kevin Cruijssen](https://codegolf.stackexchange.com/questions/69993/the-m%C3%B6bius-function/174900?noredirect=1#comment421566_174900))*  
*-1 byte: changed setting b to zero to a ternary from an if (thanks to [Kevin Cruijssen](https://codegolf.stackexchange.com/questions/69993/the-m%C3%B6bius-function/174900?noredirect=1#comment421571_174900))*  
*-7 bytes: changed if statement in for loop to a ternary (thanks to [Peter Taylor](https://codegolf.stackexchange.com/questions/69993/the-m%C3%B6bius-function/174900?noredirect=1#comment421625_174900) and [Kevin Cruijssen](https://codegolf.stackexchange.com/questions/69993/the-m%C3%B6bius-function/174900?noredirect=1#comment421632_174900))*
Ungolfed:
```
a => {
    int b = 1, i = 1;           // initialize b and i to 1
    for(; ++i <= a;)            // loop from 2 (first prime) to a
        b = a % i < 1 ?                     // ternary: if a is divisible by i
            ((a /= i) % i < 1 ? 0 : -b) :   // if true: set b to 0 if a is divisible by i squared, otherwise flip sign of b
            b;                              // if false: don't change b
    return b;                   // return b (positive for even numbers of primes, negative for odd, zero for squares)
}
```
[Answer]
# Pyth, 11
```
*{IPQ^_1lPQ
```
[Test suite](https://pyth.herokuapp.com/?code=%2a%7BIPQ%5E_1lPQ&input=10&test_suite=1&test_suite_input=1%0A2%0A3%0A4%0A5%0A6%0A7%0A8%0A9%0A10&debug=0)
This multiplies the boolean value of if the prime factors are squarefree by `-1` to the power of the number of prime factors that the number has.
`I` is an invariance check on the preceeding operator, which here is `{`, which is the unique-ify operator.
[Answer]
# Japt, 21 bytes
```
V=Uk)¬¥Vâ ¬?Vl v ªJ:0
```
[Test it online!](http://ethproductions.github.io/japt?v=master&code=Vj1VaymspVbiIKw/VmwgdiCqSjow&input=NQ==)
[Answer]
# Julia, 66 bytes
```
n->(f=factor(n);any([values(f)...].>1)?0:length(keys(f))%2>0?-1:1)
```
This is a lambda function that accepts an integer and returns an integer. To call it, assign it to a variable.
Ungolfed:
```
function µ(n::Int)
    # Get the prime factorization of n as a Dict with keys as primes
    # and values as exponents
    f = factor(n)
    # Return 0 for non-squarefree, otherwise check the length of the list
    # of primes
    any([values(f)...] .> 1) ? 0 : length(keys(f)) % 2 > 0 ? -1 : 1
end
```
[Answer]
# PARI/GP, 7 bytes
```
moebius
```
Sadly `möbius` is not available. :)
[Answer]
## Seriously, ~~19~~ 18 bytes
```
,w;`iX1=`Mπ)l1&τD*
```
[Try it online!](http://seriously.tryitonline.net/#code=LHc7YGlYMT1gTc-AKWwxJs-ERCo&input=NA)
Explanation:
```
,w;`iXDY`Mπ)l1&τD*
,w;                push two copies of the full prime factorization of the input
                      (a list of [base, exponent] pairs)
   `    `M         map the following function:
    iX1=             flatten list, discard, equal to 1
                        (push 1 if exponent == 1 else 0)
          π)       product of list, push to bottom of stack
            1&     push 1 if the # of prime factors is even else 0
              τD   double and decrement (transform [0,1] to [-1,1])
                *  multiply
```
[Answer]
# Julia, 35 bytes
```
\(n,k=1)=k%n>0?n\-~k-k\0^(n%k):1÷n
```
Based on [@xnor's Python answer](https://codegolf.stackexchange.com/a/70024). [Try it online!](http://julia.tryitonline.net/#code=XChuLGs9MSk9ayVuPjA_blwtfmsta1wwXihuJWspOjHDt24KCmZvciBuIGluIDE6NzcKICAgIEBwcmludGYoIiUyZCAtPiAlMmRcbiIsIG4sIFwobikpCmVuZA&input=)
[Answer]
# Seriously, 11 bytes
Golfing suggestions welcome. [Try it online!](http://seriously.tryitonline.net/#code=O3k7bDB-4oG_Kc-APSo&input=MjMxMA)
```
;y;l0~ⁿ)π=*
```
**Ungolfing**
```
     Implicit input n.
;    Duplicate n.
y    Push a list of the distinct prime factors of n. Call it dpf.
;    Duplicate dpf.
l    Push len(dpf).
0~   Push -1.
ⁿ    Push (-1)**len(dpf).
)    Rotate (-1)**len(dpf) to BOS. Stack: dpf, n, (-1)**len(dpf)
π    Push product(dpf).
=    Check if product(dpf) == n.
      This is only true if n is squarefree.
*    Multiply (n is squarefree) by (-1)**len(dpf).
     Implicit return.
```
[Answer]
# GNU sed, 89 bytes
```
#!/bin/sed -rf
s/.*/factor &/e
s/.*://
/\b([0-9]+) \1\b/!{
s/[0-9]+//g
s/$/1/
s/  //g
y/ /-/
}
s/ .*/0/
```
[Answer]
# J, 18 bytes
```
[:*/3<:@|2+2<._&q:
```
[Answer]
## Microsoft Office Excel, British version, 196 bytes
```
=IF(ROW()>=COLUMN(),IF(AND(ROW()=1,COLUMN()=1),1,IF(COLUMN()=1,
-SUM(INDIRECT(ADDRESS(ROW(),2)&":"&ADDRESS(ROW(),ROW()))),
IF(MOD(ROW(),COLUMN())=0,INDIRECT(ADDRESS(FLOOR(ROW()/COLUMN(),1),
1)),""))),"")
```
Excel cell formula to be entered in cells A1 to AX50.
[Answer]
## Javascript (ES6), 48 bytes
```
f=(n,i=1)=>n-1?n%++i?f(n,i):(n/=i)%i?-f(n,i):0:1
```
First of all - this is my first code golf post so I may misunderstand the rules to some extent. In this submission the last character `;` could be omitted and it'll still work but I'm not even sure if the code would be valid according to the ES6 specs. Anyways, a short explanation. 
First, I'll explain the idea a bit; we take `n`, and we try dividing it by the integer `i`. If it's divisible, then we do so and we check if it's divisible by `i` again. If that's the case, then we need to return `0`. Otherwise, we can try another `i`. The cool thing is, we can just start at `i=2` and just add increase it by `1` every time, so that we divide out all the prime factors.
So, the code works like this:
```
f=(n,i=1)=>                                           We will increase i by one at the start of
                                                         the function body, so default is 1
           n-1?                                       Check if n==1.
               n%++i?                                 If i isn't, increase i by 1, check if n
                                                         is divisible by i
                     f(n,i):                          if it isn't, we check the next i
                            (n/=i)%i?                 if it is, divide n by i, and check for
                                                         divisibility by i again
                                     -f(n,i):         if it not, then we flip the value to
                                                         account for the 1 and -1 depending on the
                                                         amount of factors
                                             0:       if it's divisible by i twice, done.
                                               1      if we're at 1, divided out all factors,
                                                         then we return 1. The line with
                                                         -f(n,i) will then take care of the sign
```
So, that's my submission. 
[Answer]
# [Haskell](https://www.haskell.org/), 96 bytes
```
import Data.Numbers.Primes
import Data.List
m=(\f->last$0:[(-1)^length f|f==nub f]).primeFactors
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/PzO3IL@oRMElsSRRz680Nym1qFgvoCgzN7WYC1nKJ7O4hCvXViMmTdcuJ7G4RMXAKlpD11AzLic1L70kQyGtJs3WNq80SSEtVlOvAKTfLTG5JL@o@H9uYmaeLVAkr0QlV8HQ6D8A "Haskell – Try It Online")
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 47 [bitsv1](https://github.com/Vyxal/Vyncode/blob/main/README.md), 5.875 bytes
```
ǐÞu$†Ǎ*
```
[Try it Online!](https://vyxal.pythonanywhere.com/?v=1#WyI9IiwiIiwix5DDnnUk4oCgx40qIiwiIiwiNzciXQ==)
What do you mean it's not morbin' time? I love vyncode and how just re-arranging things makes the program shorter.
## Explained
```
ǐÞu$†Ǎ*
ǐ       # prime factors of input with duplicates
 Þu     # all unique?
   $†Ǎ  # -1 ** len(prime_factors(input))
      * # multiply
```
]