""""
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
中对数字>;0:Sieve of Ertosthenes: Prime numbers和Factors of a number:输出:
相关问题 更多 >
编程相关推荐