Решение на Генератори от Дарина Нейчева

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

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

Резултати

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

Код

import math
def primes():
number = 2
while True:
limit = int(math.sqrt(number)) + 1
if all([number % divisor != 0 for divisor in range(2, limit)]):
yield number
number += 1
def is_prime(number):
limit = int(math.sqrt(number)) + 1
for x in range(2, limit):
if number % x == 0:
return False
return True
def semiprimes():
number = 2
while True:
r = range(2, number)
for x in r:
for y in r:
if number / x == y and is_prime(x) and is_prime(y):
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-10ospg7", 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-10ospg7", line 2029, in test_thousands_of_semi_primes
    self.assertEqual(ten_thousand_semiprimes[i], next(primes))
AssertionError: 35 != 38

----------------------------------------------------------------------
Ran 4 tests in 0.017s

FAILED (failures=2)

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

Дарина обнови решението на 30.04.2012 22:11 (преди над 8 години)

+import math
+
+
+def primes():
+ number = 2
+ while True:
+ limit = int(math.sqrt(number)) + 1
+ if all([number % divisor != 0 for divisor in range(2, limit)]):
+ yield number
+ number += 1
+
+
+def is_prime(number):
+ limit = int(math.sqrt(number)) + 1
+ for x in range(2, limit):
+ if number % x == 0:
+ return False
+ return True
+
+
+def semiprimes():
+ number = 2
+ while True:
+ r = range(2, number)
+ for x in r:
+ for y in r:
+ if number / x == y and is_prime(x) and is_prime(y):
+ yield number
+ number += 1
+ number += 1