Hey Dave,<br>Thank you for the heads up. I actually bashed my head against the desk a few times and eventually I realized what I was doing wrong. Here's my final code (slightly optimized) that's verified and working. Out of curiousity, what other optimizations could I throw at it (without diving too deep too fast).<br>
<br>#Assignment 1a<br>#Determine the 1000th prime number<br>candidate=1<br>#Already know that 2 is prime<br>primeCount=1<br>while (primeCount<=1000):<br> isPrime="true"<br> i=2<br> halfCand=(candidate/2)<br>
while (isPrime=="true") and (i<=halfCand):<br> if ((candidate%i)==0):<br> isPrime="false"<br> else:<br> i+=1<br> if isPrime=="true":<br>
print(candidate, "is a prime.")<br> primeCount+=1<br> #else:<br> #print(candidate, " is not a prime.")<br> candidate+=2<br>print("The 1000th prime number is ",(candidate-2))<br>
<br><br><div class="gmail_quote">On Tue, Aug 10, 2010 at 8:51 AM, Dave Angel <span dir="ltr"><<a href="mailto:davea@ieee.org">davea@ieee.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div></div><div class="h5">Matty Sarro wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Hey Everyone,<br>
I'm currently trying to work through MIT's opencourseware and am using<br>
python. The second assignment they offer is to determine the 1000th prime<br>
number. Below is the code I am using:<br>
<br>
#Assignment 1a<br>
#Determine the 1000th prime number<br>
candidate=3<br>
#Already know that 2 is prime<br>
primeCount=1<br>
while (primeCount<=1000):<br>
for i in range (2,candidate):<br>
if ((candidate%i)==0):<br>
print(candidate, " is not a prime")<br>
else:<br>
print(candidate, " is a prime!")<br>
primeCount+=1<br>
candidate+=2<br>
<br>
<br>
<br>
<br>
Now I'm not looking for a solution, but I'm hoping that someone can at least<br>
tell me where the error in my logic is.<br>
The outer loop keeps count and will keep iterating until the 1000th prime<br>
number has been found.<br>
The inner loop just attempts to divide the candidate number by each possible<br>
factor until it's reached, and then increases the candidate number value by<br>
two since even numbers above 2 aren't prime.<br>
The if statement inside the inner loop simply checks if there is a remainder<br>
when attempting to divide the candidate by the possible factor. If there<br>
isn't, its a factor and we can print "not a prime". If there is always a<br>
remainder, nothing is a factor and so the candidate is a prime.<br>
<br>
I figured it seemed simple enough, but I keep getting a massive output and<br>
almost nothing listed is a correct prime number.<br>
<br>
Please be gentle, its my first post and I haven't programmed in ages :)<br>
-Matty<br>
<br>
<br>
</blockquote></div></div>
Once you discover a particular value is not a prime, you need to get out of that for loop. Add a break after the appropriate print.<br>
<br>
Also, the print that says it IS a prime is misplaced. You only know that if you've gone all the way through the loop without ever hitting the break. That's a candidate for the 'else' clause of the for loop.<br>
<br>
There are other changes you could make for efficiency, but get it working correctly first.<br>
<br>
DaveA<br>
<br>
</blockquote></div><br>