Share your knowledge at the LQ Wiki.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org [SOLVED] Help with Python Prime Number Generator!
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 04-09-2011, 09:06 PM #1 MostViktorious LQ Newbie   Registered: Oct 2010 Location: Toronto, Canada Distribution: Arch Linux Posts: 25 Rep: Help with Python Prime Number Generator! I'm trying to create a python number generator, shown here: http://paste.pound-python.org/show/5029/ I get the error when running this, that "maximum recursion limit reached". I was told to do this iteratively, as opposed to recursively. How can I do that? Many thanks in advance!
 04-09-2011, 09:13 PM #2 kurumi Member   Registered: Apr 2010 Posts: 228 Rep: Code: ```>>> import sys >>> help(sys.setrecursionlimit)```
 04-10-2011, 02:19 PM #3 bgeddy Senior Member   Registered: Sep 2006 Location: Liverpool - England Distribution: slackware64 13.37 and -current, Dragonfly BSD Posts: 1,810 Rep: You shouldn't be looking at altering the built in recursion limit as your instructions were to do this iteratively. The code you linked to is inherently recursive and also very bad in a number of areas. In that code the recursive loop increments the value to be checked stored in query by one, obviously all primes are odd numbers so are at least two apart - (aside from the first being 2 and 3). Also the code appends to a list while iterating over it. That's always a bad idea and odd things can happen. You should look at putting the for loop inside a while loop,( while True or while query<=maximum_to_check), and rather than calling not_a_prime increment the value held in query by two and break out of the for loop returning up to the while loop with break. Also insert a break in if primes == known_primes[-1]: segment after appending to the list of known_primes and incrementing query by 2. All this will make the code iterative rather than recursive. I have put together a solution using the code you linked to and with the changes I highlighted but as this is presumably a homework problem it's much better that you work on this yourself. I suggest adopting the while query<=maximum_to_check and calling prime_loop(known_primes[-1],10000) for example to find primes up to 10000. You'll need to add a parameter to prime_loop to take care of this. Last edited by bgeddy; 04-10-2011 at 02:21 PM. 1 members found this post helpful.
 04-10-2011, 06:57 PM #4 graemef Senior Member   Registered: Nov 2005 Location: Hanoi Distribution: Fedora 13, Ubuntu 10.04 Posts: 2,379 Rep: You can shorten the limit of the loop checking for the prime by setting the maximum_to_check be the (square root of the number checking) + 1.
04-11-2011, 09:59 PM   #5
MostViktorious
LQ Newbie

Registered: Oct 2010
Distribution: Arch Linux
Posts: 25

Original Poster
Rep:
Quote:
 Originally Posted by bgeddy You shouldn't be looking at altering the built in recursion limit as your instructions were to do this iteratively. The code you linked to is inherently recursive and also very bad in a number of areas. In that code the recursive loop increments the value to be checked stored in query by one, obviously all primes are odd numbers so are at least two apart - (aside from the first being 2 and 3). Also the code appends to a list while iterating over it. That's always a bad idea and odd things can happen. You should look at putting the for loop inside a while loop,( while True or while query<=maximum_to_check), and rather than calling not_a_prime increment the value held in query by two and break out of the for loop returning up to the while loop with break. Also insert a break in if primes == known_primes[-1]: segment after appending to the list of known_primes and incrementing query by 2. All this will make the code iterative rather than recursive. I have put together a solution using the code you linked to and with the changes I highlighted but as this is presumably a homework problem it's much better that you work on this yourself. I suggest adopting the while query<=maximum_to_check and calling prime_loop(known_primes[-1],10000) for example to find primes up to 10000. You'll need to add a parameter to prime_loop to take care of this.
Thank you very much for a detailed answer. Unfortuantely, my Thunderbird wasn't updating my email for some reason, so I only saw this solution today. I did the exact same thing by nesting the for loop into a while loop which runs however many times I want it to. I also read before that changing the recursionlimit isn't a good idea, so thus my need for finding a much better implementation with iteration. Thank you for everyone who also helped me on this, this is a good thread to reference back on

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post amar_tk Programming 3 08-23-2010 10:35 AM LXer Syndicated Linux News 0 05-20-2009 07:00 AM sachitha Programming 2 10-20-2006 02:47 PM Chrax Programming 9 12-23-2004 01:02 AM zetsui Linux - Software 3 08-23-2003 01:23 PM

LinuxQuestions.org

All times are GMT -5. The time now is 01:13 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -