| | |
Problem with Python Loop
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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,065
Reputation:
Solved Threads: 299
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
Views: 320 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Python
anti assignment avogadro beginner bluetooth code convert count csv decimals def dictionary dynamic dynamically enter examples excel file float format frange ftp function gnu gui heads homework import input jaunty java lapse leftmouse line lines linux list lists loop microcontroller module mouse multiple newb number numbers output parsing path pointer port prime program programming projects py2exe pygame pygtk pyopengl pyqt python random raw_input recursion recursive redirect scrolledtext slicenotation software sqlite ssh stderr string strings subprocess syntax table tennis terminal text thread threading time tkinter tlapse tooltip tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape windows word wx.wizard wxpython






