【发布时间】:2021-05-14 18:52:06
【问题描述】:
""""
A function that takes a positive integer n as input and prints its prime factorization to
the screen.
Gather the factors together into a single string, so that the results of a call like
(60) would be to print the string “60 = 2 x 2 x 3 x 5” to the screen.
Looking for a way to build prime_factorization such that every factor you find will be prime
automatically.
"""
def prime_factorization(n):
results = 0
for i in range(1, n + 1):
if (n % i) == 0:
prime = True
for x in range(2, i):
if (i % x) == 0:
prime = False
if prime:
results = results + i
return results
prime_factorization(60)
以上是我对这个问题的尝试。我试图先找到这些因素,然后确定它们是否是素数。我非常坚持这一点,如果有任何帮助或建议,我将不胜感激!
【问题讨论】:
-
我不想使用内置的 Python 算法。我正在尝试制作自己的函数来打印字符串。
-
欢迎来到 Stack Overflow。请编辑您的问题以澄清:您的算法的哪些部分不起作用以及您希望得到什么问题的回答:为什么您的特定代码不起作用,或者如何计算一般的素数分解。另请参阅stackoverflow.com/help/how-to-ask。