Reputation: 10179
I am trying to rerun a loop in python when it's on the last iteration but it's not working. Here's my code:
primes = [i for i in range(2, 1001)]
p = 2
for i in range(p, len(primes), p):
primes[i] = 0
if i == len(primes)-1 and (p+1) <= max(primes):
p += 1
print(p)
print(primes)
The code only runs once as the print(primes)
shows that every 2nd value is made 0. I want it to rerun until p is not equal to the maximum value in the list.
Upvotes: 0
Views: 117
Reputation: 56654
primes = [2, 3]
upto = 1000
for num in range(4, upto+1):
if all(num % p for p in primes):
primes.append(num)
print(primes)
Note that this could still be improved by not testing for division by primes > sqrt(num)
Upvotes: 2