gpt4 book ai didi

python - 您将如何实现除数函数?

转载 作者:太空宇宙 更新时间:2023-11-03 12:33:55 25 4
gpt4 key购买 nike

divisor function是自然数的约数之和。

我做了一点研究发现 this如果你想找到给定自然数 N 的除数函数,这是一个非常好的方法,所以我尝试用 Python 编写它:

def divisor_function(n):
"Returns the sum of divisors of n"
checked = [False]*100000
factors = prime_factors(n)
sum_of_divisors = 1 # It's = 1 because it will be the result of a product
for x in factors:
if checked[x]:
continue
else:
count = factors.count(x)
tmp = (x**(count+1)-1)//(x-1)
sum_of_divisors*=tmp
checked[x]=True
return sum_of_divisors

它工作得很好,但我相信它可以改进(例如:我创建了一个包含 100000 元素的列表,但我没有使用其中的大部分)。

您将如何改进/实现它?

附言这是 prime_factors:

def prime_factors(n):
"Returns all the prime factors of a positive integer"
factors = []
d = 2
while (n > 1):
while (n%d==0):
factors.append(d)
n /= d
d = d + 1
if (d*d>n):
if (n>1): factors.append(int(n));
break;
return factors

最佳答案

在计算除​​数之和时,您需要以 p1k< 的形式对 n 进行因式分解sub>1 p2k2 ... — 也就是说,您需要分解中每个素数的指数。目前,您正在通过计算素数的平面列表来执行此操作,然后调用 count 来计算指数。这是浪费时间,因为您首先可以轻松地以您需要的格式生成质因数分解,如下所示:

def factorization(n):
"""
Generate the prime factorization of n in the form of pairs (p, k)
where the prime p appears k times in the factorization.

>>> list(factorization(1))
[]
>>> list(factorization(24))
[(2, 3), (3, 1)]
>>> list(factorization(1001))
[(7, 1), (11, 1), (13, 1)]
"""
p = 1
while p * p < n:
p += 1
k = 0
while n % p == 0:
k += 1
n //= p
if k:
yield p, k
if n != 1:
yield n, 1

上面代码注释:

  1. 我已转换此代码,使其生成分解,而不是构建列表(通过重复调用 append)并返回它。在 Python 中,这种转换几乎总是一种改进,因为它允许您在生成元素时一个一个地使用它们,而不必将整个序列存储在内存中。

  2. 这是 doctests 需要的那种功能工作顺利。

现在计算除数之和非常简单:无需存储已检查的因子集或计算每个因子出现的次数。事实上,您只需一行即可完成:

from operator import mul

def sum_of_divisors(n):
"""
Return the sum of divisors of n.

>>> sum_of_divisors(1)
1
>>> sum_of_divisors(33550336) // 2
33550336
"""
return reduce(mul, ((p**(k+1)-1) // (p-1) for p, k in factorization(n)), 1)

关于python - 您将如何实现除数函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14359936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com