title: Python recursive function error: "maximum recursion depth exceeded"
body: <p>I solved Problem 10 of Project Euler with the following code, which works through brute force:</p>
  
  <pre><code>def isPrime(n):
  
      for x in range(2, int(n**0.5)+1):
          if n % x == 0:
              return False
      return True
  
  
  def primeList(n):
  
      primes = []
  
      for i in range(2,n):
          if isPrime(i):
              primes.append(i)
  
      return primes
  
  
  def sumPrimes(primelist):
      prime_sum = sum(primelist)
      return prime_sum
  
  
  print (sumPrimes(primeList(2000000)))
  </code></pre>
  
  <p>The three functions work as follows: </p>
  
  <ol>
  <li><strong>isPrime</strong> checks whether a number is a prime;</li>
  <li><strong>primeList</strong> returns a list containing a set of prime numbers for a certain range with limit 'n', and; </li>
  <li><strong>sumPrimes</strong> sums up the values of all numbers in a list. (This last function isn't needed, but I liked the clarity of it, especially for a beginner like me.)</li>
  </ol>
  
  <p>I then wrote a new function, <strong>primeListRec</strong>, which does exactly the same thing as <strong>primeList</strong>, to help me better understand recursion:</p>
  
  <pre><code>def primeListRec(i, n):
      primes = []
      #print i
  
  
      if (i != n):
          primes.extend(primeListRec(i+1,n))
  
      if (isPrime(i)):
          primes.append(i)
          return primes
  
  
      return primes
  </code></pre>
  
  <p>The above recursive function worked, but only for very small values, like '500'. The function caused my program to crash when I put in '1000'. And when I put in a value like '2000', Python gave me this: </p>
  
  <p><strong>RuntimeError: maximum recursion depth exceeded</strong>.</p>
  
  <p>What did I do wrong with my recursive function? Or is there some specific way to avoid a recursion limit? </p>