2021年2月10日星期三

PYTHON / How do I write a function that takes a positive integer and prints its prime factorization?

""""  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)  

Above is my attempt at the problem. I tried to find the factors first and then determine if they are prime. I'm extremely stuck on this and would appreciate any help or suggestions!

https://stackoverflow.com/questions/66147166/python-how-do-i-write-a-function-that-takes-a-positive-integer-and-prints-its February 11, 2021 at 08:33AM

没有评论:

发表评论