Решение на Генератори от Светослав Атанасов

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

Към профила на Светослав Атанасов

Резултати

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

Код

from math import sqrt
def primes():
number = 2
while True:
if all([number % divisor != 0
for divisor in range(2, int(sqrt(number)) + 1)]):
yield number
number += 1
def get_primes(upper_limit):
primes_list = []
prime = primes()
value = next(prime)
while value <= upper_limit:
primes_list.append(value)
value = next(prime)
return primes_list
def semiprimes():
prime = primes()
value = next(prime)
min_current_semiprime = 0
current_list = []
while True:
max_current_semiprime = value * value
max_needed_primes_list = get_primes(max_current_semiprime // 2)
for number in range(min_current_semiprime, max_current_semiprime):
for divisor in max_needed_primes_list:
if number / divisor in max_needed_primes_list:
current_list.append(number)
current_list = list(set(current_list))
current_list.sort()
current_list.reverse()
while len(current_list) != 0:
yield current_list.pop()
min_current_semiprime = max_current_semiprime
value = next(prime)

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

....
----------------------------------------------------------------------
Ran 4 tests in 0.099s

OK

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

Светослав обнови решението на 23.04.2012 03:10 (преди над 8 години)

+from math import sqrt
+
+def primes():
+ number = 2
+ while True:
+ if all([number % divisor != 0
+ for divisor in range(2, int(sqrt(number)) + 1)]):
+ yield number
+ number += 1
+
+def get_primes(upper_limit):
+ primes_list = []
+ prime = primes()
+ value = next(prime)
+ while value <= upper_limit:
+ primes_list.append(value)
+ value = next(prime)
+ return primes_list
+
+def semiprimes():
+ prime = primes()
+ value = next(prime)
+ min_current_semiprime = 0
+ current_list = []
+ while True:
+ max_current_semiprime = value * value
+ max_needed_primes_list = get_primes(max_current_semiprime // 2)
+ for number in range(min_current_semiprime, max_current_semiprime):
+ for divisor in max_needed_primes_list:
+ if number / divisor in max_needed_primes_list:
+ current_list.append(number)
+ current_list = list(set(current_list))
+ current_list.sort()
+ current_list.reverse()
+ while len(current_list) != 0:
+ yield current_list.pop()
+ min_current_semiprime = max_current_semiprime
+ value = next(prime)