title: Memory error in Python for loops
body: <p>I'm currently trying to find the largest prime number contained within another large number.</p>
  
  <pre><code>maxlen = 1024
  for i in range(1023, -1, -1):
      maxlen -= 1
      number = ""
      for k in range(maxlen, -1, -1):
              number = pi[k] + number
              if isprime(number) == True:
                      print number
  </code></pre>
  
  <p>isprime() is a function that checks if the number is a prime (pretty standard).
  this works pretty well up to a certain point where I get a MemoryError.</p>
  
  <p>This is not because the number checked by the function is too large since it happens around the 6th run of the first for loop.</p>
  
  <p>I've already tried gc.enable() and gc.collect() without any positive result.</p>
  
  <p>Does anyone have an idea how to fix this?</p>
  
  <p>Edit: definition of pi and isprime() as per request:</p>
  
  <pre><code>f = open("/root/number", "r")
  pi = f.read()
  f.close()
  </code></pre>
  
  <p>where the file "number" contains the original number in which I'd like to find the prime.</p>
  
  <pre><code>def isprime(n):
      n = abs(int(n))
      if n &lt; 2:
              return False
      if n == 2:
              return True
      if not n &amp; 1:
              return False
      for x in range(3, int(n**0.5)+1, 2):
              if n % x == 0:
                      return False
      return True
  </code></pre>
  
  <p>Traceback:</p>
  
  <pre><code>Traceback (most recent call last):
    File "./primal.py", line 36, in &lt;module&gt;
      if isprime(number) == True:
    File "./primal.py", line 24, in isprime
      for x in range(3, int(n**0.5)+1, 2):
  MemoryError
  </code></pre>