mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-20 05:29:48 +00:00
Added Mobius Function (#1058)
* Add files via upload * Update mobius_function.py * Add files via upload * Add files via upload * Add files via upload * Add files via upload * Add files via upload * Add files via upload * Update mobius_function.py * Delete mobius_function.py * Add files via upload
This commit is contained in:
parent
93fdc9f2a1
commit
c964d743b6
39
maths/is_square_free.py
Normal file
39
maths/is_square_free.py
Normal file
@ -0,0 +1,39 @@
|
||||
"""
|
||||
References: wikipedia:square free number
|
||||
python/black : True
|
||||
flake8 : True
|
||||
"""
|
||||
from typing import List
|
||||
|
||||
|
||||
def is_square_free(factors: List[int]) -> bool:
|
||||
"""
|
||||
# doctest: +NORMALIZE_WHITESPACE
|
||||
This functions takes a list of prime factors as input.
|
||||
returns True if the factors are square free.
|
||||
>>> is_square_free([1, 1, 2, 3, 4])
|
||||
False
|
||||
|
||||
These are wrong but should return some value
|
||||
it simply checks for repition in the numbers.
|
||||
>>> is_square_free([1, 3, 4, 'sd', 0.0])
|
||||
True
|
||||
|
||||
>>> is_square_free([1, 0.5, 2, 0.0])
|
||||
True
|
||||
>>> is_square_free([1, 2, 2, 5])
|
||||
False
|
||||
>>> is_square_free('asd')
|
||||
True
|
||||
>>> is_square_free(24)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: 'int' object is not iterable
|
||||
"""
|
||||
return len(set(factors)) == len(factors)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
43
maths/mobius_function.py
Normal file
43
maths/mobius_function.py
Normal file
@ -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 maths.prime_factors import prime_factors
|
||||
from maths.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()
|
Loading…
x
Reference in New Issue
Block a user