943,813 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 550
  • Python RSS
Sep 21st, 2009
0

Problem with Python Loop

Expand Post »
Hello,

This is driving me crazy so here it goes.

I'm trying to create a program that finds the 1000th prime number. This is the code I started with to check if a number is prime:

Python Syntax (Toggle Plain Text)
  1. numberofprimesfound = 0
  2. numbertotest = 303
  3. divisor = 2
  4.  
  5.  
  6. if numbertotest%divisor == 0:
  7. print numbertotest, 'Is not a prime (broke at stage 1)'
  8.  
  9. else:
  10. while divisor < numbertotest:
  11. divisor = divisor +1
  12. if numbertotest%divisor == 0:
  13. print numbertotest, 'Is not a prime (broke at stage 2)'
  14. numbertotest = numbertotest +2
  15. break
  16. elif numbertotest%divisor <> 0:
  17. print numbertotest, 'Is a prime'
  18. numbertotest = numbertotest +2
  19. numberofprimesfound = numberofprimesfound +1
  20. break

This works correctly to check if any number is a prime above 3.

However when I add a while loop to find the 1000th prime it all goes wrong.

Python Syntax (Toggle Plain Text)
  1. numberofprimesfound = 0
  2. numbertotest = 5
  3. divisor = 2
  4.  
  5. while numberofprimesfound < 1000:
  6.  
  7. if numbertotest%divisor == 0:
  8. numbertotest = numbertotest +2
  9. divisor = 2
  10.  
  11. else:
  12.  
  13. while divisor < numbertotest:
  14. divisor = divisor +1
  15.  
  16. if numbertotest%divisor == 0:
  17. numbertotest = numbertotest +2
  18. divisor = 2
  19. break
  20.  
  21. elif numbertotest%divisor <> 0:
  22. numbertotest = numbertotest +2
  23. numberofprimesfound = numberofprimesfound +1
  24. divisor = 2
  25. break
  26.  
  27. print 'The', numberofprimesfound,'th prime is', numbertotest

The loop reports the 1000th prime to be 3003 which obviously isnt right. But I cant figure out why when the prime check works fine it all breaks when I simply add a while loop.

Any ideas?

Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
racshot65 is offline Offline
3 posts
since Dec 2007
Sep 21st, 2009
0

Re: Problem with Python Loop

At first, I would adopt consistent indentations! Very important in Python. Most people use 4 spaces.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Sep 21st, 2009
0

Re: Problem with Python Loop

Done

Python Syntax (Toggle Plain Text)
  1. numberofprimesfound = 0
  2. numbertotest = 5
  3. divisor = 2
  4.  
  5. while numberofprimesfound < 1000:
  6.  
  7. if numbertotest%divisor == 0:
  8. numbertotest = numbertotest +2
  9. divisor = 2
  10.  
  11. else:
  12.  
  13. while divisor < numbertotest:
  14. divisor = divisor +1
  15.  
  16. if numbertotest%divisor == 0:
  17. numbertotest = numbertotest +2
  18. divisor = 2
  19. break
  20.  
  21. elif numbertotest%divisor <> 0:
  22. numbertotest = numbertotest +2
  23. numberofprimesfound = numberofprimesfound +1
  24. divisor = 2
  25. break
  26.  
  27. print 'The', numberofprimesfound,'th prime is', numbertotest
Reputation Points: 10
Solved Threads: 0
Newbie Poster
racshot65 is offline Offline
3 posts
since Dec 2007
Sep 21st, 2009
0

Re: Problem with Python Loop

Note that you add 2 to numbertotest before you break, so the prime would have been 3001 (don't know if that really is a prime).
Python Syntax (Toggle Plain Text)
  1. ## assume numberofprimesfound = 999
  2. while numberofprimesfound < 1000:
  3. .
  4. .
  5. .
  6. elif numbertotest%divisor <> 0:
  7. numbertotest = numbertotest +2
  8. numberofprimesfound = numberofprimesfound +1
  9. divisor = 2
  10. break
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Sep 21st, 2009
0

Re: Problem with Python Loop

Your method of finding primes is flawed. Try 25, 35 and 49. None of those numbers are prime and it says that they are. Fix your indentation and your algorithm and then try to loop it again. I suggest using a function.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Displaying Deck of Cards
Next Thread in Python Forum Timeline: Help with server side (cgi) python scripting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC