A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
1st homework... Trying to count the 1000th prime. Keep getting lost in a loop.
Please help!
anonymous
 5 years ago
1st homework... Trying to count the 1000th prime. Keep getting lost in a loop. Please help!

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0divisor = 3 primecounter = 1 nprime = 0 nthprime = int(raw_input('how many prime numbers shall we count? ')) while nprime < nthprime: ##still haven't reached our limit if primecounter%2 == 0: ## if it's even.. throw away primecounter += 2 else: while divisor < primecounter/2: ## no divisors past this if primecounter%divisor != 0: ## there is a remainder divisor += 2 ##try another divisor elif primecounter%divisor == 0: ## no remainder not prime primecounter += 2 ## try again with a higher number divisor = 1 else: ## no divisors found. Number is prime nprime += 1 ##account for prime number primecounter += 2 ##repeat test for new number divisor = 1 ##reset divisor else: print 'the ',nthprime,'th prime number is:',primecounter

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0i feel like i'm close... but can't seem to find out where my problem is. Thanks in advance for any help

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0im taking a look at it now, ill go through my steps to debugging as i go. 1st: divided the program up by adding in print statements, narrow down the location of the problem.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0i placed a print statement in each of the while statements, notice the program never gets to the second while statement, infinite loops occurs with the first one

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0this statement is never true while divisor < primecounter/2:

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0the first part of code that is redundant is the first if else statement, as long as you increment by +2 it will never be an even number

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0if you look at your if else statement, you can notice if the first statement is true then the second one is false and vice versa, ie it will never reach the final else statement.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0good man. seems like i got a lot of work to. And yes the even number statement I should have realized. When you say the first statement is true then the second one is false I am not entirely sure where you are looking. Thanks a bunch for the help

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0if primecounter%divisor != 0: ## there is a remainder elif primecounter%divisor == 0: ## no remainder not prime else: ## will never execute

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0You cannot know if a divisor was found until your innermost while loop ends. You need to end your inner while loop as soon as you find a divisor, then have another if to check to see if you found a divisor or not. If so, it's prime, if not, then move on.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0you can accomplish what polpak is saying by using a flag. This is something that you can use to show that you have or haven't found something. i e found = true

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Alternatively you could break out of the loop when you find a divisor, then check again outside the while loop if primecounter%divisor != 0 then you didn't break early and it's a prime.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0ok this is great help. Seems that the way I roadmapped should be improved. And I need to create better parameters for my inner while loop to make sure it gets out of it. Let's see if I can't make this happen. Thanks again to both of yall. I'll post some more again when I either make it happen or my brain gets lost in its own infinite loop

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0polpak whats your position on using break tho i have always thought of it as bad programming pratice

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Break is ok, though return is better. If I were doing this, I'd have a function to test for the 'primeness' of a number (returning true/false). That function would handle the inner while loop. Then to find the 1000th prime is just a matter of looping through all the odds, until 999 of them were prime. If I wanted to do it without a function, I'd make the modulo against divisor part of my inner while predicate. e.g. while (divisor < primecounter/2) and (divisor%primecounter != 0): divisor += 2

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0actually, in the case of having a function, that while loop would just be in the function, so I'd do that in both cases ;p

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0yea i think that is the best answer for this also. thanks

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0In general, I feel that break is ok. But I also feel that nested loops sould often be avoided, so that may be why I don't have much of a problem with break. The cases where break is often viewed as problematic are in situations where you have a couple of levels of nesting and it's not clear which ones you're breaking out of.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.