MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / mobius

Function mobius

maths/mobius_function.py:12–37  ·  view source on GitHub ↗

Mobius function >>> mobius(24) 0 >>> mobius(-1) 1 >>> mobius('asd') Traceback (most recent call last): ... TypeError: '<=' not supported between instances of 'int' and 'str' >>> mobius(10**400) 0 >>> mobius(10**-400) 1 >>> mo

(n: int)

Source from the content-addressed store, hash-verified

10
11
12def mobius(n: int) -> int:
13 """
14 Mobius function
15 >>> mobius(24)
16 0
17 >>> mobius(-1)
18 1
19 >>> mobius('asd')
20 Traceback (most recent call last):
21 ...
22 TypeError: '<=' not supported between instances of 'int' and 'str'
23 >>> mobius(10**400)
24 0
25 >>> mobius(10**-400)
26 1
27 >>> mobius(-1424)
28 1
29 >>> mobius([1, '2', 2.0])
30 Traceback (most recent call last):
31 ...
32 TypeError: '<=' not supported between instances of 'int' and 'list'
33 """
34 factors = prime_factors(n)
35 if is_square_free(factors):
36 return -1 if len(factors) % 2 else 1
37 return 0
38
39
40if __name__ == "__main__":

Callers

nothing calls this directly

Calls 2

prime_factorsFunction · 0.90
is_square_freeFunction · 0.90

Tested by

no test coverage detected