| | |
Problem with Python Loop
![]() |
•
•
Join Date: Dec 2007
Posts: 2
Reputation:
Solved Threads: 0
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:
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.
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
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)
numberofprimesfound = 0 numbertotest = 303 divisor = 2 if numbertotest%divisor == 0: print numbertotest, 'Is not a prime (broke at stage 1)' else: while divisor < numbertotest: divisor = divisor +1 if numbertotest%divisor == 0: print numbertotest, 'Is not a prime (broke at stage 2)' numbertotest = numbertotest +2 break elif numbertotest%divisor <> 0: print numbertotest, 'Is a prime' numbertotest = numbertotest +2 numberofprimesfound = numberofprimesfound +1 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)
numberofprimesfound = 0 numbertotest = 5 divisor = 2 while numberofprimesfound < 1000: if numbertotest%divisor == 0: numbertotest = numbertotest +2 divisor = 2 else: while divisor < numbertotest: divisor = divisor +1 if numbertotest%divisor == 0: numbertotest = numbertotest +2 divisor = 2 break elif numbertotest%divisor <> 0: numbertotest = numbertotest +2 numberofprimesfound = numberofprimesfound +1 divisor = 2 break 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
•
•
Join Date: Dec 2007
Posts: 2
Reputation:
Solved Threads: 0
Done 

Python Syntax (Toggle Plain Text)
numberofprimesfound = 0 numbertotest = 5 divisor = 2 while numberofprimesfound < 1000: if numbertotest%divisor == 0: numbertotest = numbertotest +2 divisor = 2 else: while divisor < numbertotest: divisor = divisor +1 if numbertotest%divisor == 0: numbertotest = numbertotest +2 divisor = 2 break elif numbertotest%divisor <> 0: numbertotest = numbertotest +2 numberofprimesfound = numberofprimesfound +1 divisor = 2 break print 'The', numberofprimesfound,'th prime is', numbertotest
•
•
Join Date: Dec 2006
Posts: 1,008
Reputation:
Solved Threads: 285
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)
## assume numberofprimesfound = 999 while numberofprimesfound < 1000: . . . elif numbertotest%divisor <> 0: numbertotest = numbertotest +2 numberofprimesfound = numberofprimesfound +1 divisor = 2 break
Linux counter #99383
![]() |
Similar Threads
- Problem with python and i dont know what to do.... (Python)
- Problem with loop, infinite or no loop at all (Java)
- problem in python (Python)
- Python problem (Python)
- My first Python Program, it's too messy (Python)
- XP Startup Problem: Infinite Loop (Windows NT / 2000 / XP)
- problem in python. how to do this...?! (Python)
- problem with MATLAB loop (Legacy and Other Languages)
Other Threads in the Python Forum
- Previous Thread: Displaying Deck of Cards
- Next Thread: Help with server side (cgi) python scripting
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming progressbar projects py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython






