Python Forum
Twin Primes Recursion Limit
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Twin Primes Recursion Limit
#1
I'm working on this same problem too and ran into an error trying to get the twin prime numbers to work. I get a recursion error when checking the prime list.
def primeCheck(n):
    prime_list = [2]
    twin_prime_list = [2]
    for number in range(3, n, 2):
        if all(number % i != 0 for i in range(2, int(number ** .5) + 1)):
            prime_list.append(number)
        if primeCheck(n) and primeCheck(n + 2):
            twin_prime_list.append(n , n + 2)
    return prime_list
    print(twin_prime_list)       
Reply
#2
This looks sufficiently different from the other thread (the other one was about displaying primes in vertically aligned tables), so I've split it into it's own thread.

With that said, I'll need to have some time to play around with it to see if I can help, but normally there's some conditions on whether or not to recurse.  As it is now, the function will call itself every time, as long as n is greater than 3.

In the meantime, could you do us a favor and define what it is, exactly, that a "twin prime" is?  If it's just a number, and that number+2 both being prime, then it would be a very, very small list, right?
Reply
#3
Twin primes are prime numbers two less than or greater than another prime. It is not a very small list. It is quite possibly an infinitely large list, but that has not been proven one way or the other yet.

I would not use recursion for checking prime numbers, I would use a loop. If you do use recursion, you need to pass the prime number list when you recurse. As it is now you are rebuilding the entire prime number list each time you call the function. Furthermore, you are recursing on the same value that was passed to the function. This will lead to infinite recursion and cause you problems in any language, even one optimized for recursion (which is Python is not).

So I would use a loop, and keep track of the prime number list. Every time you find a prime number, you can check if the previous prime number found is two less than the prime you just found. If it is, then you have twin primes.

Edit: If you just replace the recursive calls to the function on line 7 with checks that n and n - 2 are in prime_list, then it should work. Also note that 2 is not a twin prime, so you shouldn't start twin_prime_list with it. Finally, you can only append one item, not two, so line 8 will fail. Make a tuple or a list out of the twin primes and append that instead.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Recursion doesn't seem like a good way to approach this I think.
Write a sieve to return all primes in the desired range.
Then iterate through that and append ones to a new list that have a twin.
Reply
#5
This is an extension of the list of primes program from another thread. I have to check the list of primes for twin primes then print the primes and twin primes in separate tables.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  GCF function w recursion and helper function(how do i fix this Recursion Error) hhydration 3 2,561 Oct-05-2020, 07:47 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020