class PrimeNumbers:
def __init__(self, n):
self.prime_numbers = []
self.i = 0
self.n = n
def __iter__(self):
self.i = 0
return self
def __next__(self):
self.i += 1
for number in range(2, self.n + 1):
for prime in self.prime_numbers:
if number % prime == 0:
break
else:
self.prime_numbers.append(number)
raise StopIteration()
prime_number_iterator = PrimeNumbers(n=10000)
for number in prime_number_iterator:
print(number)
class PrimeNumbers:
def __init__(self, n):
self.prime_numbers = []
self.n = n
self.i = 0
def __iter__(self):
self.i = 1
return self
def get_prime_numbers(self):
self.i += 1
for prime in self.prime_numbers:
if self.i % prime == 0:
return False
return True
def __next__(self):
while self.i < self.n:
if self.get_prime_numbers():
self.prime_numbers.append(self.i)
return self.i
else:
raise StopIteration()