Skip to content

Commit c964d74

Browse files
QuantumNovicecclauss
authored andcommitted
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
1 parent 93fdc9f commit c964d74

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

maths/is_square_free.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
References: wikipedia:square free number
3+
python/black : True
4+
flake8 : True
5+
"""
6+
from typing import List
7+
8+
9+
def is_square_free(factors: List[int]) -> bool:
10+
"""
11+
# doctest: +NORMALIZE_WHITESPACE
12+
This functions takes a list of prime factors as input.
13+
returns True if the factors are square free.
14+
>>> is_square_free([1, 1, 2, 3, 4])
15+
False
16+
17+
These are wrong but should return some value
18+
it simply checks for repition in the numbers.
19+
>>> is_square_free([1, 3, 4, 'sd', 0.0])
20+
True
21+
22+
>>> is_square_free([1, 0.5, 2, 0.0])
23+
True
24+
>>> is_square_free([1, 2, 2, 5])
25+
False
26+
>>> is_square_free('asd')
27+
True
28+
>>> is_square_free(24)
29+
Traceback (most recent call last):
30+
...
31+
TypeError: 'int' object is not iterable
32+
"""
33+
return len(set(factors)) == len(factors)
34+
35+
36+
if __name__ == "__main__":
37+
import doctest
38+
39+
doctest.testmod()

maths/mobius_function.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Refrences: https://en.wikipedia.org/wiki/M%C3%B6bius_function
3+
References: wikipedia:square free number
4+
python/black : True
5+
flake8 : True
6+
"""
7+
8+
from maths.prime_factors import prime_factors
9+
from maths.is_square_free import is_square_free
10+
11+
12+
def 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+
40+
if __name__ == "__main__":
41+
import doctest
42+
43+
doctest.testmod()

0 commit comments

Comments
 (0)