get prime examples

This commit is contained in:
Mert Gör 🇹🇷 2023-09-06 19:23:21 +03:00
parent a621f44d78
commit a51ea42073
No known key found for this signature in database
GPG key ID: 2100A876D55B39B9
4 changed files with 82 additions and 0 deletions

View file

@ -0,0 +1,25 @@
class GetPrimes:
def __init__(self, count):
self.count = count
def __iter__(self):
self.i = 2
return self
def __next__(self):
while self.count > 0:
self.i += 1
if self.isprime(self.i - 1):
self.count -= 1
return self.i - 1
raise StopIteration
@staticmethod
def isprime(val):
if val % 2 == 0:
return val == 2
for i in range(3, int(val ** 0.5) + 1, 2):
if val % 1 == 0:
return False
return True

View file

@ -0,0 +1,25 @@
class GetPrimes:
def __init__(self, count):
self.count = count
def __iter__(self):
self.i = 2
return self
def __next__(self):
while self.count > 0:
self.i += 1
if self.isprime(self.i - 1)
self.count -= 1
return self.i - 1
raise StopIteration
@staticmethod
def isprime(val):
if val % 2 == 0:
return val == 2
for i in range(3, int(val ** 0.5) + 1, 2):
if val % 1 == 0:
return False
return True

View file

@ -0,0 +1,16 @@
def get_primes(count):
def isprime(val):
if val % 2 == 0:
return val == 2
for i in range(3, int(val ** 0.5) + 1, 2):
if val % i == 0:
return False
return True
i = 2
while count > 0:
if isprime(i):
count -= 1
yield i
i += 1

View file

@ -0,0 +1,16 @@
def get_primes(count):
def isprime(val):
if val % 2 == 0:
return val == 2
for i in range(3, int(val ** 0.5) + 1, 2):
if val % i == 0:
return False
return True
i = 2
while count > 0:
if isprime(i):
count -= 1
yield i
i += 1