gpt4 book ai didi

python - 寻找主要因素的差异

转载 作者:太空狗 更新时间:2023-10-29 18:09:15 25 4
gpt4 key购买 nike

在使用 Python 时 primefac模块 - https://pypi.org/project/primefac/

我注意到这段代码有效:

import sys
import primefac
n = 600851475143
factors = list(primefac.primefac(n))

但这不是:

import sys
import primefac
n = 19087688894909892783503691960213776632781962588843842839953893606139157282825376128877238229887486797933180624979637419997128020864299273315243907454874577263432419226852240380380880131843664800828228959920799327101817796594944161768692639537839544009100224905464911818390882192901883104039350105285757995782376058970382205463192526628231366854662473466838863987148898819243940809068605863725041711337107340279029811816555169181781669826715177100102639379572663639848699896757952171115689208069972249342540932428107175784150214806633479073061672324629925288020557720111253896992657435200329511186117042808357973613389
factors = list(primefac.primefac(n))

导致如下错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\primefac.py", line 677, in primefac
f = multifactor(n, methods=methods, verbose=verbose)
File "C:\Python27\lib\site-packages\primefac.py", line 596, in multifactor
for p in procs: p.start()
File "C:\Python27\lib\multiprocessing\process.py", line 130, in start
self._popen = Popen(self)
File "C:\Python27\lib\multiprocessing\forking.py", line 277, in __init__
dump(process_obj, to_child, HIGHEST_PROTOCOL)
File "C:\Python27\lib\multiprocessing\forking.py", line 199, in dump
ForkingPickler(file, protocol).dump(obj)
File "C:\Python27\lib\pickle.py", line 224, in dump
self.save(obj)
File "C:\Python27\lib\pickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "C:\Python27\lib\pickle.py", line 425, in save_reduce
save(state)
File "C:\Python27\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python27\lib\pickle.py", line 655, in save_dict
self._batch_setitems(obj.iteritems())
File "C:\Python27\lib\pickle.py", line 687, in _batch_setitems
save(v)
File "C:\Python27\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python27\lib\pickle.py", line 754, in save_global
(obj, module, name))
pickle.PicklingError: Can't pickle <function factory at 0x00000000032520B8>: it's not found as primefac.factory
type(n)Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python27\lib\multiprocessing\forking.py", line 381, in main
self = load(from_parent)
File "C:\Python27\lib\pickle.py", line 1384, in load
return Unpickler(file).load()
File "C:\Python27\lib\pickle.py", line 864, in load
dispatch[key](self)
File "C:\Python27\lib\pickle.py", line 886, in load_eof
raise EOFError
EOFError

有人知道为什么会这样吗?

在这两种情况下 type(n)返回 <type 'long'>

最佳答案

factory 函数在 primefac.py 中的另一个函数 multifactor 中定义。

pickle.PicklingError: Can't pickle function factory at 0x00000000032520B8: it's not found as primefac.factory

Pickle 仅适用于顶级函数。

如果您将此函数移动到顶层,即 primefac.py 中的 multifactor 之外,则此错误将消失。

关于python - 寻找主要因素的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54253612/

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