Решение на Генератори от Щилиян Харбов

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

Към профила на Щилиян Харбов

Резултати

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

Код

import math
def is_prime(number):
start = 3
end = math.sqrt(number)
while start <= end:
if number % start == 0: return False
start = start + 2
return True
def primes():
primes_candidate = list(range(3, 1000000, 2))
primes_candidate.insert(0, 2)
for num in primes_candidate:
if is_prime(num): yield num
def semi_primes():
pi = primes()
p = []
p.append(next(pi))
i = 0
while True:
j = i
while j >= 0:
yield p[i] * p[j]
j = j - 1
p.append(next(pi))
i = len(p) - 1

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

.E.E
======================================================================
ERROR: test_first_few_semi_primes (__main__.PrimesTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-11891-p7y9o6", line 2017, in test_first_few_semi_primes
    primes = self.solution.semiprimes()
AttributeError: 'module' object has no attribute 'semiprimes'

======================================================================
ERROR: test_thousands_of_semi_primes (__main__.PrimesTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-11891-p7y9o6", line 2027, in test_thousands_of_semi_primes
    primes = self.solution.semiprimes()
AttributeError: 'module' object has no attribute 'semiprimes'

----------------------------------------------------------------------
Ran 4 tests in 0.065s

FAILED (errors=2)

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

Щилиян обнови решението на 03.05.2012 18:55 (преди над 12 години)

+import math
+
+def is_prime(number):
+ start = 3
+ end = math.sqrt(number)
+ while start <= end:
+ if number % start == 0: return False
+ start = start + 2
+ return True
+
+
+def primes():
+ primes_candidate = list(range(3, 1000000, 2))
+ primes_candidate.insert(0, 2)
+ for num in primes_candidate:
+ if is_prime(num): yield num
+
+def semi_primes():
+ pi = primes()
+ p = []
+ p.append(next(pi))
+ i = 0
+ while True:
+ j = i
+ while j >= 0:
+ yield p[i] * p[j]
+ j = j - 1
+ p.append(next(pi))
+ i = len(p) - 1
+