在factorial分解程序中找不到bug

时间:2013-03-19 17:51:42

标签: python prime-factoring

我正在尝试编写一个找到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 

我已经看了好几个小时了,我不知道出了什么问题......

1 个答案:

答案 0 :(得分: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'>]

对这些操作可能会溢出。如果我将pe转换为int

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数组索引的方便性具有任意精度,则可以使用object的dtype,即

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

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