简体   繁体   English

寻找素因子的差异

[英]Difference in finding prime factors

While working with the Python primefac module - https://pypi.org/project/primefac/ 使用Python primefac模块时 - https://pypi.org/project/primefac/

I noticed that this code works: 我注意到这段代码有效:

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

But this doesn't: 但这不是:

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

Resulting in the following error: 导致以下错误:

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

Does anybody know why is this happening? 有人知道为什么会这样吗?

In both cases type(n) returns <type 'long'> 在这两种情况下, type(n)返回<type 'long'>

Here are my two cents: 这是我的两分钱:

  • Try and import gmpy2 , if you don't already have it. 尝试并导入gmpy2 ,如果你还没有它。
  • From the project page you linked to: 从您链接到的项目页面:

    GNU's factor command won't factor anything greater than 2^127-1 GNU的factor命令不会计算大于2 ^ 127-1的任何因子

    Which is roughly 1.7 * 10^38, a significantly smaller number then the one you are being "dumped" on. 大概是1.7 * 10 ^ 38,比你被“倾倒”的数字少得多。 So, it might be (I am speculating here) that there are limitations in that package and that people who report it working on some OS (MacOS, as of this moment) are also getting some "dump" error, that is handled using the OS on the CPython level, with some "junk" memory values, leading them to believe that this is working. 因此,它可能(我在这里推测)该包中存在限制,并且报告它在某些操作系统(MacOS,此时)上工作的人也会收到一些“转储”错误,这是使用CPython级别的操作系统,带有一些“垃圾”内存值,使他们相信这是有效的。

The function factory is defined inside another function multifactor in primefac.py . 函数factoryprimefac.py中的另一个函数multifactor定义

pickle.PicklingError: Can't pickle function factory at 0x00000000032520B8: it's not found as primefac.factory pickle.PicklingError:无法在0x00000000032520B8处挑选函数工厂:找不到primefac.factory

Pickle works on top level functions only. Pickle仅适用于顶级功能。

If you move this function to top level ie out of multifactor in primefac.py , then this error will be gone. 如果将此函数移动到顶级,即在primefac.py中的多重因子之外 ,则此错误将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM