0

我正在尝试编写一个找到 n 的素数分解的程序!. 我之前已经成功完成了,但是我找不到我已经编写的代码,所以我必须重写它!:p 这里是代码:

import math                                                                                                                                                 
import numpy as np
import itertools as it
import operator as op

def primes(n):
    """A simple sieve to find the primes less than n"""                                                                                                                                              
    nums = np.arange(3,n+1,2)                                                                                                                               
    sqrtn = int(n**0.5)/2                                                                                                                                   
    for step in nums[:sqrtn]:                                                                                                                               
        if step:
            nums[step*step/2-1::step]=0
    return [2] + map(int, filter(None, nums))

def factFactors(n):
    """Finds the prime factorization of n! using the property found
    here: http://en.wikipedia.org/wiki/Factorial#Number_theory"""                                                                               
    ps = primes(n)                                                                                                                                        
    for p in ps:                                                                                                                             
        e = 0                                                                                                                                               
        for i in it.count(1):                                                                                                                                
            epeice = n/(p**i)                                                                                                                               
            if epeice == 0: break
            e += epeice                                                                                                                                     
        yield p, e                                                                                                                                               

if __name__=="__main__":
    x = list(factFactors(100))
    print x, reduce(op.mul, [p**e for p, e in x], 1)==math.factorial(100)

输出是这样的:

[(2, 97), (3, 48), (5, 24), (7, 16), (11, 9), (13, 7), (17, 5), (19, 5), (23, 4), (29, 3), (31, 3), (37, 2), (41, 2), (43, 2), (47, 2), (53, 1), (59, 1), (6
1, 1), (67, 1), (71, 1), (73, 1), (79, 1), (83, 1), (89, 1), (97, 1)] False 

我一直在看这个几个小时,我不知道出了什么问题......

4

1 回答 1

1

这段代码在我试验时更改了多次,但当前版本的一个问题是,因为factFactors它是一个生成器,

x = factFactors(100)                                       
print list(x), reduce(op.mul, [p**e for p, e in x], 1)==math.factorial(100)

调用list将耗尽生成器,因此reduce没有什么可采取的行动。改为使用x = list(factFactors(100))

-

更正result/results错字后(好吧,当我开始写这篇文章时就已经存在了!)我无法运行代码:

~/coding$ python2.7 factbug4.py
factbug4.py:31: RuntimeWarning: overflow encountered in long_scalars
  print x, reduce(lambda a, b: a*b, [p**e for p, e in x], 1)==math.factorial(100)
[(2, 97), (3, 48), (5, 24), (7, 16), (11, 9), (13, 7), (17, 5), (19, 5), (23, 4), (29, 3), (31, 3), (37, 2), (41, 2), (43, 2), (47, 2), (53, 1), (59, 1), (61, 1), (67, 1), (71, 1), (73, 1), (79, 1), (83, 1), (89, 1), (97, 1)]
Traceback (most recent call last):
  File "factbug4.py", line 31, in <module>
    print x, reduce(lambda a, b: a*b, [p**e for p, e in x], 1)==math.factorial(100)
  File "factbug4.py", line 31, in <lambda>
    print x, reduce(lambda a, b: a*b, [p**e for p, e in x], 1)==math.factorial(100)
TypeError: unsupported operand type(s) for *: 'long' and 'numpy.int32'

但它确实暗示了问题可能是什么。(由于代码不会为我运行,我不能确定,但​​我有理由确定。)返回的大多数元素primes不是 Python 任意精度整数,而是有限范围的 numpy 整数:

>>> primes(10)
[2, 3, 5, 7]
>>> map(type, primes(10))
[<type 'int'>, <type 'numpy.int32'>, <type 'numpy.int32'>, <type 'numpy.int32'>]

并且对它们的操作可能会溢出。如果我转换p为:eint

print x, reduce(lambda a, b: a*b, [int(p)**int(e) for p, e in x], 1)==math.factorial(100)

我明白了

[(2, 97), (3, 48), (5, 24), (7, 16), (11, 9), (13, 7), 
(17, 5), (19, 5), (23, 4), (29, 3), (31, 3), (37, 2), 
(41, 2), (43, 2), (47, 2), (53, 1), (59, 1), (61, 1), 
(67, 1), (71, 1), (73, 1), (79, 1), (83, 1), (89, 1), (97, 1)] True

如果你想方便numpy任意精度的数组索引,你可以使用 dtype object,即

>>> np.arange(10,dtype=object)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=object)

但老实说,我建议不要numpy在这里使用。

于 2013-03-19T18:28:42.837 回答