I am trying the following to generate the prime numbers till a given limit. I thought of using sieve of eratosthenes. When I use a list I get the correct output but when I don't make it as a list the output is false and gives back all numbers. Is there a way to do this with recursion and since there is a recursion limit imposed in python is there any other way to use the below structure. I read through Rosetta code to check for implementations that use a set and also with a list where they set the number as not prime with a dictionary.
def prime_till(number):
seq = range(2, number)
while True:
try:
first = seq.__next__()
except AttributeError:
first = seq.__iter__().__next__()
except StopIteration:
return None
yield first
seq = list(filter(lambda x: x % first != 0, seq))
print(list(prime_till(20)))
Output :
[2, 3, 5, 7, 11, 13, 17, 19]
Without list :
def prime_till(number):
seq = range(2, number)
while True:
try:
first = seq.__next__()
except AttributeError:
first = seq.__iter__().__next__()
except StopIteration:
return None
yield first
seq = filter(lambda x: x % first != 0, seq)
print(list(prime_till(20)))
Output :
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]