在python中打印数字的唯一因子

在python中打印数字的唯一因子,python,algorithm,optimization,Python,Algorithm,Optimization,我有下面的代码,它打印给定数字的因子的唯一组合。 但是我没有给出期望的输出。代码如下所示: #Print the factors list for the given number def print_factors_list(dividend, factorstring, predivisor): """This function takes a number and prints the factors""" divisor = dividend - 1 for i in range(div

我有下面的代码,它打印给定数字的因子的唯一组合。 但是我没有给出期望的输出。代码如下所示:

#Print the factors list for the given number
def print_factors_list(dividend, factorstring, predivisor):
"""This function takes a number and prints the factors"""
divisor = dividend - 1
for i in range(divisor, 1, -1 ):
    if dividend % i != 0:
        continue

    if i > predivisor:
        continue

    quotient = dividend / i


    if quotient <= i:
        if quotient <= predivisor:
            print factorstring + str(i) + "*" + str(quotient)
    print_factors_list(quotient, str(factorstring) + str(i) + "*", i)
    #Check if the number is greater than 0
    def print_factors(x):
    # Check if the number is greater than 0 
    if (x < 0):
       print "Enter a positive interger"
    #Go to the function print_factors_list for further evaluation
    else:
    print_factors_list(x, str(x) + "*" + str(1) + "\n", x )

    #Take input from the user
    num = int(input("Enter a number: "))
    print_factors(num)
32*1
16*2
32*1
8*4
32*1
8*2*2
32*1
4*4*2
32*1
4*2*2*2
32*1
2*2*2*2*2
我应该

 32*1
 16*2
 8*4
 8*2*2
 4*4*2
 4*2*2*2
 2*2*2*2*2

我是一个巨蟒爱好者,所以犯了一些愚蠢的错误。请有人帮我解释一下我在逻辑上哪里出错了。谢谢。

你需要一个数的所有可能因子还是只需要素数分解

如果是前者,您应该能够从后者轻松构建它。我衷心鼓励使用Symphy,python的符号数学库

from sympy import *
primefactors(5551) #provides which primes are included
[7, 13, 61]
factorint(5551) #provides how often each prime occurs
{7: 1, 13: 1, 61: 1}
那么这就是一个组合问题了

另外,请参见

下面是三个组成素数得到所有可能因子的情况

prime=6329487
pl=primefactors(prime)
pp=factorint(prime)
for i in xrange(1+pp[pl[0]]):
    for j in xrange(1+pp[pl[1]]):
        for k in xrange(1+pp[pl[2]]):
            print (pl[0]**i)*(pl[1]**j)*(pl[2]**k)

我得到的答案如下。我喜欢所有的因素组合,但不是唯一的。多谢各位

#Python program to print out all ways to multiply smaller integers 
#that equal the original number, without repeating sets of factors



def print_factors_list(dividend, factorstring, predivisor):
"""This function takes a number and prints the factors"""


   divisor = dividend - 1
   #Looping to get the factors
   for i in range(divisor, 1, -1 ):

        if dividend % i != 0:
        continue

        if i > predivisor:
        continue
        #Get the quotient for the multipying number   
        quotient = dividend / i


        if quotient <= i:
           if quotient <= predivisor:
           print factorstring + str(i) + "*" + str(quotient)
        print_factors_list(quotient, str(factorstring) + str(i) + "*", i)




#Check if the number is greater than 0
def print_factors(x):
# Check if the number is greater than 0 
    if (x < 0):
       print "Enter a positive integer"
    #Check if the number is 0
    elif (x == 0):
        print "Enter a positive integer"
    #Go to the function print_factors_list for further evaluation
    else:
    #Print the default factors(the number itself and 1)
       print str(x) + "*" + str(1)
    print_factors_list(x, "", x )




#Take input from the user
num = int(input("Enter a number: "))
#Call the function with the number given by the user
print_factors(num)
#Python程序,用于打印出小整数乘法的所有方法
#等于原始数,不重复因子集
def打印系数列表(股息、系数字符串、预测):
“”“此函数获取一个数字并打印系数”“”
除数=被除数-1
#循环获取因子
对于范围内的i(除数,1,-1):
如果股息%i!=0:
持续
如果i>predivisor:
持续
#获取多重数的商
商=股息/i

如果需要,请仔细检查代码格式。Markdown打乱了缩进,这会杀死任何Python代码;请修复缩进错误。我对可能的因素感兴趣。我将在下面讨论如何找到一个数的素数分区。基本方法使用动态规划。如果小于目标n的素数是a,b,…,z,那么n的素数分区是n-a的素数分区加上a,再加上n-b的素数分区加上b,再加上…,再加上n-z的素数分区加上z。动态规划首先找到2、3、4等的素数分区,一直到n。对于我输入的第一个“随机”数,“UnboundLocalError:赋值前引用的局部变量‘商’”,这是错误的:1729@msw-缩进可能不正确。但逻辑对我来说很好。