[Tutor] Question on lists

Jon Cosby jlcos@accessone.com
Thu, 18 Mar 1999 08:21:41 -0800


Hi - I'm trying to write a function that will return a list of prime factors
of a given number. For example, factor(36) would return [2,2,3,3]. What I
have so far is given below. Trouble is, each time it factors the quotient, a
is given as an empty list again. At this point, it's returning just the
first prime factor of the number. Is there any way to work around this?
Thanks in advance,

Jon Cosby

# Factor by trial division:
def factor(n):
     a = []                # Is there a better way to define a?
     k = floor(sqrt(n))
     for i in range(2, k+1):
          if n%i == 0:
             a.insert(len(a), i)
             factor(n/i)    # Factor quotient
             break
     return a