I'm trying to print out prime numbers in a certain range and decided to go with the Segmented Sieve of Eratosthenes to find prime numbers from 2 up to sqrt(n) and use those found primes in the list to print out the prime numbers from any range such that a number in the range not divisible by any of the numbers in the sieve list ends up being a prime.
My code works for small ranges such as from 2 to 10 and 3 to 5 but starts printing out duplicates and also non-primes for large n. Any help fixing this?
from math import sqrt, ceil
#Creating segmented sieve
start=2
stop=1000000000
sieve=[i for i in range(2,ceil(sqrt(stop)+1))]
for num in range(0,len(sieve)):
num2=num+1
while num2<len(sieve):
if sieve[num2]%sieve[num]==0:
sieve.pop(num2)
num2+=1
else:
num2+=1
#printing primes from start to stop using primes in sieve to ignore their multiples
for n in range(start,stop+1):
if n==sieve[0]:
print(n)
continue
if n%sieve[0]==0:
continue
else:
p=1
while p<len(sieve):
if n==sieve[p]:
print(n)
p+=1
continue
if n%sieve[p]==0:
p+=1
continue
else:
p+=1
print(n)
while
loop, why do youcontinue
altering printing a value? – Scott Huntercontinue
to increment p and check if the condition holds for the next iteration. – Tenshi