gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:05:30 24 4
gpt4 key购买 nike

<分区>

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

#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 的新手,所以犯了一些愚蠢的错误。请有人帮助我在逻辑上出错的地方。谢谢。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com