I wrote this Python code to print all the prime numbers until 1000:
primes = []
for prime in xrange(1, 1000):
for b in xrange(2, prime):
if (prime % b == 0):
break
else:
primes.append(prime)
print primes
However, if a number is a prime, it is appended a lot of times before moving to the next number. I tried using continue instead of break but that does not work.
I also added some more code onto that (which works) to simply output the array into a text file. It is so large that I cannot even paste it into here.
How can I append each prime number to the list only once instead of many times?
There's a feature in Python you can use quite effectively here. Just change your indentation like this:
That is, de-indent the
elseclause. This way it is not theelseof theifbut of theforloop. Aforloop can have anelsebranch which gets executed after the loop has performed all scheduled iterations. If there is abreakcalled, it gets not executed.You can use that here without having to change a lot of your original code.
Of course, in your original version, each loop iteration in which a tested number did not turn out to be a non-prime, that tested number got appended (which was not what you wanted).
Also note that
1is not a prime number (at least since Gauss in 1801, some say even since Euclid (600BC) first wrote about prime numbers, although there were heretics since into the 19th century), so your outer loop should start at2instead.But note what Daniel wrote in his answer; you do not have to step through all the way to
primebecause after reaching the square root ofprime, you can bail out. I always comparedb * btoprime, but maybe computingsqrt(prime)once even is faster.And of course, you can be faster by ignoring all even numbers completely. Except
2none are prime ;-)