FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
AlgorithmsInPython/maths/mobius_function.py at master · icgowtham/AlgorithmsInPython · GitHub
icgowtham
/
AlgorithmsInPython
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
AlgorithmsInPython
/
maths
/
mobius_function.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (37 loc) · 977 Bytes
Breadcrumbs
AlgorithmsInPython
/
maths
/
mobius_function.py
Copy path
File metadata and controls
43 lines (37 loc) · 977 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
References: https://en.wikipedia.org/wiki/M%C3%B6bius_function
References: wikipedia:square free number
python/black : True
flake8 : True
"""
from
maths
.
is_square_free
import
is_square_free
from
maths
.
prime_factors
import
prime_factors
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
()
Back
|
FazBrowse Home
|
New Git URL