【发布时间】:2021-05-14 17:55:54
【问题描述】:
""""
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 算法。
标签: python python-3.x python-2.7 math