443,907 Members | 1,832 Online Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

# Why does my code to find primes stop working after prime 300?

 P: 1 I am trying to use recursion to calculate the nth prime. It works up until i believe around 300 when the interpreter says "maximum recusrion depth exceeded." Here is the code i wrote. Expand|Select|Wrap|Line Numbers def program(n): ## initializes     if n == 1: ## checks for special case of even prime          print 2         return None      counter = 1     index = 3     nthprime(n, counter, index) ## calls nthprime which takes n, counter index, as arguments    def nthprime(n, counter, index):     if counter == n-1 and isprime(index) != None: ## test case         print index         return None     if isprime(index) != None: ## increments counter and sets index to next odd number if index is prime         counter = counter + 1            index = index + 2       else: index = index + 2  ## keeps counter the same and sets index to next odd number if index is not prime       nthprime(n, counter, index)  ## calls nthprime with either the new counter and index or just the new index    def isprime(x):       index = 2 ## variable to check divisors     while index <= x**.5: ## loop condition of how many divisors to check          if x%index == 0: ## if a remainder is found on first index return nothing              return None           else: index = index + 1  ## increment index and check again     return x ## no divisors found less than sqrt x, therefore number is prime Dec 17 '10 #1 