【发布时间】:2018-11-10 20:46:18
【问题描述】:
任务是编写将数字折叠成素数的函数。通过给定数字“n”,此函数应返回元组列表 p_i,c^i,例如,如果输入为 100,则输出为 (2,2),(5,2)。 所以,这就是我尝试写它的方式:
def factor(n):
c = 1
pre_ans = list()
temp_n=n
for i in range(2,temp_n+1):
if (is_prime(i) == True) and (temp_n % i == 0):
for j in range (2,temp_n+1):
if (temp_n % (i ** j) == 0):
pre_ans.append((i,j))
temp_n /= (i **j)
pre_ans.append((i,c))
temp_n /= i
print(pre_ans)
它工作错了,但我找不到错误:(
【问题讨论】:
-
is_prime 是函数:def is_prime(n): return n > 1 and all(n % i != 0 for i in range(2, n))
-
你有什么问题?
-
另外,您为什么没有将
is_prime作为正确编辑的函数包含在您的问题中?请编辑它。 -
我的问题是为什么这个确切的代码不能正常工作,尽管我发现它在意识形态上是正确的。如果我做错了,请纠正我
-
请提供给定输入的代码输出示例。
标签: python python-3.x primes factorization