Решение на Генератори от Станислав Гатев

Обратно към всички решения

Към профила на Станислав Гатев

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)

Код

def isprime(num):
remainders = (num % n for n in range(2, int(num ** 0.5 + 1)))
return num > 1 and all(remainders)
def primes():
num = 2
while True:
if isprime(num):
yield num
num += 1
def semiprimes():
num = 2
while True:
for n in range(2, int(num ** 0.5 + 1) + 1):
if not num % n and isprime(n) and isprime(num // n):
yield num
break
num += 1

Лог от изпълнението

....
----------------------------------------------------------------------
Ran 4 tests in 0.013s

OK

История (1 версия и 0 коментара)

Станислав обнови решението на 22.04.2012 21:45 (преди почти 12 години)

+def isprime(num):
+ remainders = (num % n for n in range(2, int(num ** 0.5 + 1)))
+ return num > 1 and all(remainders)
+
+
+def primes():
+ num = 2
+ while True:
+ if isprime(num):
+ yield num
+ num += 1
+
+
+def semiprimes():
+ num = 2
+ while True:
+ for n in range(2, int(num ** 0.5 + 1) + 1):
+ if not num % n and isprime(n) and isprime(num // n):
+ yield num
+ break
+ num += 1