Skip to content

Instantly share code, notes, and snippets.

@igorvanloo
Created December 16, 2021 04:45
Show Gist options
  • Select an option

  • Save igorvanloo/29c3fcfefaf51ad6353ca6cbe44e0797 to your computer and use it in GitHub Desktop.

Select an option

Save igorvanloo/29c3fcfefaf51ad6353ca6cbe44e0797 to your computer and use it in GitHub Desktop.
Möbius function
def Mobius(n):
if n == 1:
return 1
d = 2
num_of_primes = 0
while n > 1:
while n % d == 0:
num_of_primes += 1
if n % (d*d) == 0:
return 0
n /= d
d = d + 1
if d*d > n:
if n > 1:
num_of_primes += 1
break
return (-1)**num_of_primes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment