title: Recursion Issue in a Prime Factorization + Optimization
body: <p>I am creating a module for the factors of any number. In it, I also have two functions (one that leads to a calling of the other) that find the prime factorization of the number, n.</p>
  
  <p>The problem that comes up is a recursion error (if my definition of recursion is correct).
  When I call the function for a number, it prints all of the prime factors, then it adds the last two prime factors and prints it again, and does this repeatedly, to an apparent no end.</p>
  
  <p>My code so far:</p>
  
  <pre><code>def primeFactors(n):
     from primenum2 import isPrime
     from math import sqrt
     global pFact, y
     x, pFact, y = 2, [], 0
     if isPrime(n):
        return n
     else:
        while x &lt;= sqrt(n):
           if isPrime(x) and (n%x==0):
              pFact.append(x)
              y = n / x
              if isPrime(y):
                 pFact.append(y)
                 print pFact
                 break
              else:
                 primeFactors2(y)
           else:
              x += 1
  
  #NEW FUNCTION - I made two functions to avoid another recursion error that was occuring.
  
  def primeFactors2(y):
     from primenum2 import isPrime
     from math import sqrt
     z = 2
     while z &lt;= sqrt(y):
        if isPrime(z) and (y%z==0):
           pFact.append(z)
           y = y / z
           if isPrime(y):
              pFact.append(y)
              print pFact
              break
           else:
              primeFactors2(y)
        else:
           z += 1
  </code></pre>
  
  <p>When I input (in the Shell): <code>primeFactors(600851475143)</code> &lt;---This was for Project Euler originally</p>
  
  <p>Expected Output (I already solved the problem): <code>[71, 839, 1471, 6857L]</code></p>
  
  <p>Actual Output:</p>
  
  <pre><code>[71, 839, 1471, 6857L]
  [71, 839, 1471, 6857L, 1471, 6857L]
  [71, 839, 1471, 6857L, 1471, 6857L, 71, 839, 1471, 6857L]
  [71, 839, 1471, 6857L, 1471, 6857L, 71, 839, 1471, 6857L, 1471, 6857L]
  [71, 839, 1471, 6857L, 1471, 6857L, 71, 839, 1471, 6857L, 1471, 6857L, 71, 839, 1471, 6857L]
  </code></pre>
  
  <p>It does this over and over, appending 1471 and 6857L to the list then printing it again. Then, it adds all of the prime factors again then prints it again. Not sure why it does this. Any input is greatly appreciated. Also, if there are any ways to make this code faster/ more Pythonic, please tell me :) Thanks</p>