LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 04-09-2011, 10:06 PM   #1
MostViktorious
LQ Newbie
 
Registered: Oct 2010
Location: Toronto, Canada
Distribution: Arch Linux
Posts: 25

Rep: Reputation: 1
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!
 
Old 04-09-2011, 10:13 PM   #2
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 46
Code:
>>> import sys
>>> help(sys.setrecursionlimit)
 
Old 04-10-2011, 03:19 PM   #3
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 231Reputation: 231Reputation: 231
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 03:21 PM.
 
1 members found this post helpful.
Old 04-10-2011, 07:57 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 04-11-2011, 10:59 PM   #5
MostViktorious
LQ Newbie
 
Registered: Oct 2010
Location: Toronto, Canada
Distribution: Arch Linux
Posts: 25

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by bgeddy View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] prime number using sieve of atkin amar_tk Programming 3 08-23-2010 11:35 AM
LXer: Is La Toya Jackson a Prime Number? LXer Syndicated Linux News 0 05-20-2009 08:00 AM
function to find the closest prime number... sachitha Programming 2 10-20-2006 03:47 PM
C Prime Generator Chrax Programming 9 12-23-2004 02:02 AM
mersenne prime number zetsui Linux - Software 3 08-23-2003 02:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration