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

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

Към профила на Никола Робаков

Резултати

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

Код

def primes():
number = 2
while True:
if all([number % divisor for divisor in range(2, number)]):
yield number
number += 1
def semiprimes():
next_prime = primes()
need_primes = [next(next_prime)]
number = 2
while True:
if number > need_primes[-1]:
need_primes.append(next(next_prime))
for i in need_primes:
if number % i == 0:
if number / i in need_primes:
yield number
number += 1
number += 1

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

.F.F
======================================================================
FAIL: test_first_few_semi_primes (__main__.PrimesTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-11891-1k85k6x", line 2019, in test_first_few_semi_primes
    self.assertEqual(ten_thousand_semiprimes[i], next(primes))
AssertionError: 35 != 38

======================================================================
FAIL: test_thousands_of_semi_primes (__main__.PrimesTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-11891-1k85k6x", line 2029, in test_thousands_of_semi_primes
    self.assertEqual(ten_thousand_semiprimes[i], next(primes))
AssertionError: 35 != 38

----------------------------------------------------------------------
Ran 4 tests in 0.043s

FAILED (failures=2)

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

Никола обнови решението на 03.05.2012 12:33 (преди над 8 години)

+def primes():
+ number = 2
+ while True:
+ if all([number % divisor for divisor in range(2, number)]):
+ yield number
+ number += 1
+
+def semiprimes():
+ next_prime = primes()
+ need_primes = [next(next_prime)]
+ number = 2
+ while True:
+ if number > need_primes[-1]:
+ need_primes.append(next(next_prime))
+ for i in need_primes:
+ if number % i == 0:
+ if number / i in need_primes:
+ yield number
+ number += 1
+ number += 1