Skip to content

Commit

Permalink
Create MobiusFunction.py
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumNovice authored Feb 21, 2019
1 parent a0bc166 commit 490757e
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions MobiusFunction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

def is_square_free(factors):
'''
This functions takes a list of prime factors as input.
returns True if the factors are square free.
'''
for i in factors:
if factors.count(i) > 1:
return False
return True


def prime_factors(n):
'''
Returns prime factors of n as a list.
'''
i = 2
factors = []
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors.append(i)
if n > 1:
factors.append(n)
return factors

def mobius_function(n):
'''
Defines Mobius function
'''
factors = prime_factors(n)
if is_square_free(factors):
if len(factors)%2 == 0:
return 1
elif len(factors)%2 != 0:
return -1
else:
return 0

0 comments on commit 490757e

Please sign in to comment.