我需要制作一个程序来检查一个数字是否可以分成三个(或更多)数字。例如8 = 2 * 2 * 2和153 = 3 * 3 * 17,依此类推。它必须适用于所有正数实数。我无法绕过它:(
Mohan
20
1
答案 0 :(得分:0)
好了,我看到你试过了。这是你想要的吗?
从这里复制答案: Python - Integer Factorization into Primes
def factorize(num):
for possible_factor in range(2, num):
if num % possible_factor == 0:
return [possible_factor] + factorize(num // possible_factor)
return [num]
nums = [8,153]
for num in nums:
print("{}: {}".format(num, factorize(num)))
返回:
8: [2, 2, 2]
153: [3, 3, 17]