Ai
1 Star 0 Fork 0

Role/Python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
MobiusFunction.py 813 Bytes
一键复制 编辑 原始数据 按行查看 历史
Jérôme Krell 提交于 2019-10-10 20:22 +08:00 . Reformat Code by PyCharm-Community
def is_square_free(factors):
'''
This functions takes a list of prime factors as input.
returns True if the factors are square free.
'''
for i in factors:
if factors.count(i) > 1:
return False
return True
def prime_factors(n):
'''
Returns prime factors of n as a list.
'''
i = 2
factors = []
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors.append(i)
if n > 1:
factors.append(n)
return factors
def mobius_function(n):
'''
Defines Mobius function
'''
factors = prime_factors(n)
if is_square_free(factors):
if len(factors) % 2 == 0:
return 1
elif len(factors) % 2 != 0:
return -1
else:
return 0
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/CRole/Python.git
git@gitee.com:CRole/Python.git
CRole
Python
Python
master

搜索帮助