Skip to content

Commit 490757e

Browse files
Create MobiusFunction.py
1 parent a0bc166 commit 490757e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

MobiusFunction.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
def is_square_free(factors):
3+
'''
4+
This functions takes a list of prime factors as input.
5+
returns True if the factors are square free.
6+
'''
7+
for i in factors:
8+
if factors.count(i) > 1:
9+
return False
10+
return True
11+
12+
13+
def prime_factors(n):
14+
'''
15+
Returns prime factors of n as a list.
16+
'''
17+
i = 2
18+
factors = []
19+
while i * i <= n:
20+
if n % i:
21+
i += 1
22+
else:
23+
n //= i
24+
factors.append(i)
25+
if n > 1:
26+
factors.append(n)
27+
return factors
28+
29+
def mobius_function(n):
30+
'''
31+
Defines Mobius function
32+
'''
33+
factors = prime_factors(n)
34+
if is_square_free(factors):
35+
if len(factors)%2 == 0:
36+
return 1
37+
elif len(factors)%2 != 0:
38+
return -1
39+
else:
40+
return 0

0 commit comments

Comments
 (0)