Решение на Генератори от Недислав Стойчев

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

Към профила на Недислав Стойчев

Резултати

  • 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 get_primes(number):
p = primes()
prime_numbers = []
for x in range(number):
prime_numbers.append(next(p))
return prime_numbers
def semiprimes():
prime_numbers = 10
semiprimes = []
nat_counter = 100
while True:
primes = get_primes(prime_numbers)
for x in range(0, len(primes)):
for y in range(0, len(primes)):
nat_numbers = range(3, nat_counter)
if primes[x] * primes[y] in nat_numbers:
semiprimes.append(primes[x] * primes[y])
nat_counter += 100
for semiprime in sorted(set(semiprimes)):
yield semiprime
prime_numbers += 1

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

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

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

----------------------------------------------------------------------
Ran 4 tests in 0.042s

FAILED (failures=2)

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

Недислав обнови решението на 02.05.2012 20:13 (преди над 8 години)

+def primes():
+ number = 2
+ while True:
+ if all([number % divisor for divisor in range(2, number)]):
+ yield number
+ number += 1
+
+def get_primes(number):
+ p = primes()
+ prime_numbers = []
+ for x in range(number):
+ prime_numbers.append(next(p))
+ return prime_numbers
+
+def semiprimes():
+ prime_numbers = 10
+ semiprimes = []
+ nat_counter = 100
+ while True:
+ primes = get_primes(prime_numbers)
+ for x in range(0, len(primes)):
+ for y in range(0, len(primes)):
+ nat_numbers = range(3, nat_counter)
+ if primes[x] * primes[y] in nat_numbers:
+ semiprimes.append(primes[x] * primes[y])
+ nat_counter += 100
+ for semiprime in sorted(set(semiprimes)):
+ yield semiprime
+ prime_numbers += 1