Problem with Python Loop

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2007
Posts: 2
Reputation: racshot65 is an unknown quantity at this point 
Solved Threads: 0
racshot65 racshot65 is offline Offline
Newbie Poster

Problem with Python Loop

 
0
  #1
Sep 21st, 2009
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:

  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.

  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,297
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Problem with Python Loop

 
0
  #2
Sep 21st, 2009
At first, I would adopt consistent indentations! Very important in Python. Most people use 4 spaces.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 2
Reputation: racshot65 is an unknown quantity at this point 
Solved Threads: 0
racshot65 racshot65 is offline Offline
Newbie Poster

Re: Problem with Python Loop

 
0
  #3
Sep 21st, 2009
Done

  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,065
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is online now Online
Veteran Poster

Re: Problem with Python Loop

 
0
  #4
Sep 21st, 2009
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).
  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
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Problem with Python Loop

 
0
  #5
Sep 21st, 2009
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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 320 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC