Newbie question - calculating prime numbers
msarro at gmail.com
Tue Aug 10 13:57:40 CEST 2010
I'm currently trying to work through MIT's opencourseware and am using
python. The second assignment they offer is to determine the 1000th prime
number. Below is the code I am using:
#Determine the 1000th prime number
#Already know that 2 is prime
for i in range (2,candidate):
print(candidate, " is not a prime")
print(candidate, " is a prime!")
Now I'm not looking for a solution, but I'm hoping that someone can at least
tell me where the error in my logic is.
The outer loop keeps count and will keep iterating until the 1000th prime
number has been found.
The inner loop just attempts to divide the candidate number by each possible
factor until it's reached, and then increases the candidate number value by
two since even numbers above 2 aren't prime.
The if statement inside the inner loop simply checks if there is a remainder
when attempting to divide the candidate by the possible factor. If there
isn't, its a factor and we can print "not a prime". If there is always a
remainder, nothing is a factor and so the candidate is a prime.
I figured it seemed simple enough, but I keep getting a massive output and
almost nothing listed is a correct prime number.
Please be gentle, its my first post and I haven't programmed in ages :)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list