primes = [2,3,5,7]
for n in range(10, 10000):
isPrime = True
for d in primes:
if (n % d) == 0:
isPrime = False
else:
primes.append(n)
break
print(primes[n])
I have to print all the prime numbers from 10 to 10000 and then append them to the given array of first few prime numbers. My code was working earlier as I tested it multiple times. Now it throws me an an error "list index out of range". Not sure what's wrong thought I was on the right track.
I think you should start with something like this
You need to iterate over all the prime numbers before you can append to the list. Otherwise, you'll always break that loop on odd numbers (you modded with 2)
primes = [2,3,5,7]
for n in range(10, 10000):
isPrime = True
for d in primes:
if n % d == 0:
isPrime = False
break
if isPrime:
primes.append(n)
print(primes)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.