Skip to content

Added Mobius Function #1058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 21, 2019
Merged
Prev Previous commit
Next Next commit
Add files via upload
  • Loading branch information
QuantumNovice authored Jul 20, 2019
commit e00ff91099ec3140da9f35861375b7fc5fca5c7a
43 changes: 43 additions & 0 deletions mobius_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Refrences: https://en.wikipedia.org/wiki/M%C3%B6bius_function
References: wikipedia:square free number
python/black : True
flake8 : True
"""

from typing import List
from .prime_factors import prime_factors
from .is_square_free import is_square_free

def mobius(n: int) -> int:
"""
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
>>> mobius(-1424)
1
>>> mobius([1, '2', 2.0])
Traceback (most recent call last):
...
TypeError: '<=' not supported between instances of 'int' and 'list'
"""
factors = prime_factors(n)
if is_square_free(factors):
return -1 if len(factors) % 2 else 1
return 0


if __name__ == "__main__":
import doctest

doctest.testmod()