Решение на Генератори от Николай Колев

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

Към профила на Николай Колев

Резултати

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

Код

def primes():
primes = []
current_number = 2;
while True:
if all(current_number % prime != 0 for prime in primes):
primes.append(current_number)
yield current_number
current_number += 1
def semiprimes():
primes = []
current_number = 2;
while True:
divisor_count = 0
for prime in primes:
number = current_number
while number % prime == 0:
number /= prime
divisor_count += 1
if divisor_count == 0:
primes.append(current_number)
elif divisor_count == 2:
yield current_number
current_number += 1

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

....
----------------------------------------------------------------------
Ran 4 tests in 0.009s

OK

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

Николай обнови решението на 22.04.2012 19:30 (преди над 8 години)

+def primes():
+ primes = []
+ current_number = 2;
+ while True:
+ if all(current_number % prime != 0 for prime in primes):
+ primes.append(current_number)
+ yield current_number
+ current_number += 1
+
+def semiprimes():
+ primes = []
+ current_number = 2;
+ while True:
+ divisor_count = 0
+ for prime in primes:
+ number = current_number
+ while number % prime == 0:
+ number /= prime
+ divisor_count += 1
+ if divisor_count == 0:
+ primes.append(current_number)
+ elif divisor_count == 2:
+ yield current_number
+ current_number += 1