Python递归程序对数字进行质因数分解
我编写了以下程序来对数字进行质因数分解:
import math
def prime_factorize(x,li=[]):
until = int(math.sqrt(x))+1
for i in xrange(2,until):
if not x%i:
li.append(i)
break
else: #This else belongs to for
li.append(x)
print li #First print statement; This is what is returned
return li
prime_factorize(x/i,li)
if __name__=='__main__':
print prime_factorize(300) #Second print statement, WTF. why is this None
以下是我得到的输出:
[2, 2, 3, 5, 5]
None
虽然,返回值打印正确,但返回后的值似乎始终不打印任何内容。我缺少什么?
另外,我该如何改进程序(继续使用递归)
I wrote the following program to prime factorize a number:
import math
def prime_factorize(x,li=[]):
until = int(math.sqrt(x))+1
for i in xrange(2,until):
if not x%i:
li.append(i)
break
else: #This else belongs to for
li.append(x)
print li #First print statement; This is what is returned
return li
prime_factorize(x/i,li)
if __name__=='__main__':
print prime_factorize(300) #Second print statement, WTF. why is this None
Following is the output I get:
[2, 2, 3, 5, 5]
None
Altho', the returned value is printed properly, the after returned value seems to be printing none, all the time. What am I missing?
Also, how can I improve the program (continuing to use the recursion)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的 prime_factorize 函数在递归情况下没有 return 语句 - 您希望在其最后一行调用“return prime_factorize(x/i,li)”。尝试使用素数(因此不需要递归调用)以查看它在这种情况下是否有效。
另外,您可能希望使签名类似于:
否则在调用它两次或多次时会得到错误的结果:
Your prime_factorize function doesn't have a return statement in the recursive case -- you want to invoke "return prime_factorize(x/i,li)" on its last line. Try it with a prime number (so the recursive call isn't needed) to see that it works in that case.
Also you probably want to make the signature something like:
otherwise you get wrong results when calling it two or more times:
如果您想完全递归地执行此操作,我会推荐此代码,它确实返回正确的答案,并且其工作方式非常清晰。
如果您想让程序尽可能高效,我建议您坚持使用以前的方法之一。
这是解决问题的完全递归方法
If you want to do it completely recursive, I'd recommend this code, it does return the correct answer and the way it works is pretty clear.
If you want to make the program as efficient as possible I'd recommend you to stick to one of your previous methods.
This is a completely recursive way of solving your problem
@Anthony 正确回答了您关于
print
的原始问题。然而,本着还提供的几个技巧的精神,这里有一个使用尾递归删除的简单重构:这并不能解决关键的性能问题(大 O 行为与原始解决方案相同)——但是因为Python 本身不进行尾递归优化,学习手动进行尾递归优化很重要。
“将[非基本情况]递归步骤
'return thisfun(newargs)'
更改为args=newargs; continue
并将整个主体放入while中True:
loop”是尾递归优化的基本思想。在这里,我还使 li 成为非参数(没有理由让它成为参数),在while
上放置一个条件,并避免自递归以来的continue
无论如何,步骤是在身体的末端。该公式将成为应用进一步优化重构(避免平方根、记忆化……)以实现更好性能的良好基础。
@Anthony's correctly answered your original question about
print
. However, in the spirit of the several tips that were also offered, here's a simple refactorization using tail recursion removal:This doesn't address the crucial performance issues (big-O behavior is the same as for your original solution) -- but since Python itself doesn't do tail-recursion optimization, it's important to learn to do it manually.
"Change the [non-base-case] recursive steps
'return thisfun(newargs)'
intoargs=newargs; continue
and put the whole body into awhile True:
loop" is the basic idea of tail-recursion optimization. Here I've also made li a non-arg (no reason for it to be an arg), put a condition on thewhile
, and avoided thecontinue
since the recursive step was at the end of the body anyway.This formulation would be a good basis from which to apply further optimizing refactorings (sqrt avoidance, memoization, ...) to reach towards better performance.
更加实用的风格版本。
A more functional-style version.
可以吗?
is it fine.
看看这个代码
check out this code