-1

how is the same code working? click hereQuestion The question asks to create a function which returns the prime factors of a given number, given as a output Probelem It returns the string "Done" instead of the prime factors of the given number What I've tried I've tried defining the function and had also provided the code I wrote

Code

 def prime_factors(number):
      factor = 2
      while number <= factor:
        if number % factor == 0:
          print(factor)
          number = number / factor
        else:
          factor += 1
      return "Done"
    
    prime_factors(100)
  • _"It returns the string "Done""_ - that's what you asked it to do, what did you think `return "Done"` was doing? – jonrsharpe Dec 15 '21 at 18:32

2 Answers2

0

When you find a prime factor, add it to a list, and return that list (once you're done and have found all you need to find).

primes = []

... (your code)
   if you found a prime factor:
     primes.append(prime_factor)

... (more of your code)
return primes
0

When you find a prime factor, append it to the result list.

def prime_factors(number):
    factor = 2
    factors = []
    while factor * factor <= number:
        while number % factor == 0:
            factors.append(factor)
            number //= factor
        factor += 1
    if number > 2: factors.append(number)
    return factors
    
print(prime_factors(100))
Unmitigated
  • 76,500
  • 11
  • 62
  • 80