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

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

Към профила на Мирослав Милев

Резултати

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

Код

def primes():
number = 2
while True:
yield number
number = next_prime(number)
def next_prime(number):
number += 1
while not is_prime(number):
number += 1
return number
def is_prime(number):
middle = int(number/2) + 1
for i in range (2, middle):
if number % i == 0:
return False
return True
def semiprimes():
number = 4
while True:
yield number
number = next_semiprime(number)
def next_semiprime(number):
number += 1
while not is_semiprime(number):
number += 1
return number
def is_semiprime(number):
middle = int(number/2) + 1
dividers = []
for i in range (2, middle):
if number % i == 0:
dividers.append(i)
if number / i == i:
dividers.append(i)
return (len(dividers) == 2 and
is_prime(dividers[0]) and
is_prime(dividers[1]))

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

....
----------------------------------------------------------------------
Ran 4 tests in 0.016s

OK

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

Мирослав обнови решението на 28.04.2012 13:17 (преди над 12 години)

+def primes():
+ number = 2
+ while True:
+ yield number
+ number = next_prime(number)
+
+def next_prime(number):
+ number += 1
+ while not is_prime(number):
+ number += 1
+ return number
+
+def is_prime(number):
+ middle = int(number/2) + 1
+ for i in range (2, middle):
+ if number % i == 0:
+ return False
+ return True
+
+def semiprimes():
+ number = 4
+ while True:
+ yield number
+ number = next_semiprime(number)
+
+def next_semiprime(number):
+ number += 1
+ while not is_semiprime(number):
+ number += 1
+ return number
+
+def is_semiprime(number):
+ middle = int(number/2) + 1
+ dividers = []
+ for i in range (2, middle):
+ if number % i == 0:
+ dividers.append(i)
+ if number / i == i:
+ dividers.append(i)
+ return (len(dividers) == 2 and
+ is_prime(dividers[0]) and
+ is_prime(dividers[1]))