A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

1st homework... Trying to count the 1000th prime. Keep getting lost in a loop. Please help!

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    divisor = 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

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i feel like i'm close... but can't seem to find out where my problem is. Thanks in advance for any help

  3. mattc
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    im 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.

  4. mattc
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i 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

  5. mattc
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    this statement is never true while divisor < primecounter/2:

  6. mattc
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    the 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

  7. mattc
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    if 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.

  8. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    good 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

  9. mattc
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    if primecounter%divisor != 0: ## there is a remainder elif primecounter%divisor == 0: ## no remainder- not prime else: ## will never execute

  10. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    You 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.

  11. mattc
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    you 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

  12. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Alternatively 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.

  13. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ok 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

  14. mattc
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    polpak whats your position on using break tho i have always thought of it as bad programming pratice

  15. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Break 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

  16. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    actually, 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

  17. mattc
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yea i think that is the best answer for this also. thanks

  18. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    In 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.

  19. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.