We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a0bc166 commit 490757eCopy full SHA for 490757e
MobiusFunction.py
@@ -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
40
+ return 0
0 commit comments