用Python打印一个数字的唯一因子

1 投票
2 回答
1535 浏览
提问于 2025-04-18 12:43

我有一段代码,它可以打印出一个给定数字的独特因子组合。

但是它没有给出我想要的结果。代码如下:

#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

我还是个Python新手,所以可能犯了一些低级错误。请问有人能帮我看看我的逻辑哪里出错了吗?谢谢。

2 个回答

0

我找到这个问题的答案了。其实我当时是在寻找所有的因子组合,而不是唯一的组合。谢谢大家。

#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)
1

你是想要一个数字的所有可能因子,还是只想要它的质因数分解呢?

如果你需要所有因子,那你可以很轻松地从质因数分解中得到它们。我非常推荐使用sympy,这是一个用于符号计算的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}

接下来,这就变成了一个组合问题。

另外,可以看看 Python因数分解

这里是用三个质数来获取所有可能因子的示例。

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)

撰写回答