get prime examples
This commit is contained in:
parent
a621f44d78
commit
a51ea42073
4 changed files with 82 additions and 0 deletions
25
python-temel/get_primes.iterable.py
Normal file
25
python-temel/get_primes.iterable.py
Normal 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
|
25
python-temel/get_primes.iterable.py~
Normal file
25
python-temel/get_primes.iterable.py~
Normal 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
|
16
python-temel/get_primes.py
Normal file
16
python-temel/get_primes.py
Normal 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
|
||||||
|
|
16
python-temel/get_primes.py~
Normal file
16
python-temel/get_primes.py~
Normal 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
|
||||||
|
|
Loading…
Reference in a new issue