title: IndexError: list index out of range--- Prime Factor Generator
body: <p>I have a program that finds the prime factors of any number, n. When running it, I get an index error because the index is exceeding the limit (where the limit is sqrt(n)). I'm not sure why it exceeds the limit. Can anyone provide any insight?</p>
  
  <p>My code works well for most numbers: </p>
  
  <pre><code>&gt;&gt;&gt; pFactors(250000)
  [2, 2, 2, 2, 5, 5, 5, 5, 5, 5]
  &gt;&gt;&gt; pFactors(123456789)
  [3, 3, 3607, 3803]
  &gt;&gt;&gt; pFactors(123456)
  
  Traceback (most recent call last):
    File "&lt;pyshell#2&gt;", line 1, in &lt;module&gt;
      pFactors(123456)
    File "D:\my_stuff\Google Drive\Modules\factors.py", line 50, in pFactors
      check = primes[index]
  IndexError: list index out of range
  &gt;&gt;&gt; pFactors(123455)
  
  Traceback (most recent call last):
    File "&lt;pyshell#3&gt;", line 1, in &lt;module&gt;
      pFactors(123455)
    File "D:\my_stuff\Google Drive\Modules\factors.py", line 50, in pFactors
      check = primes[index]
  IndexError: list index out of range
  </code></pre>
  
  <p>Oddly, so far I've only found it unable to work for numbers 123400-1234</p>
  
  <p>Here is my code: </p>
  
  <pre><code>def pFactors(n):
     import primes as p
     from math import sqrt
     global pFact
     pFact, primes, limit, check, num, index = [], [], int(round(sqrt(n))), 2, n, 0
     if type(n) != int and type(n) != long:
        raise TypeError("Argument &lt;n&gt; can only be &lt;type 'int'&gt; or &lt;type 'long'&gt;")
     else:
        if p.isPrime(n):
           pFact = [1, n]
        else:
           p.prevPrimes(limit)
           for i in p.primes_dict:
              if p.primes_dict[i]:
                 primes.append(i)
           while check &lt;= limit:
              if check in primes and (num%check==0):
                 pFact.append(check)
                 num = num / check
                 if num in primes:
                    pFact.append(num)
                    break
              else:
                 check = primes[index]
                 index += 1
        return pFact
  </code></pre>
  
  <p>I am sure that the problem doesn't lie in the <code>primes.py</code>, as this works fine. If anyone has any solutions on how to fix this, please tell me. Thanks!</p>