Решение на Генератори от Методи Димитров

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

Към профила на Методи Димитров

Резултати

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

Код

from math import sqrt
def primes(iterations_limiter = 0):
"""
Generator for prime numbers.
"""
following_number = 2
while 1:
number_below_following = round(sqrt(following_number), 0)
is_prime = True
while number_below_following > 1:
if not following_number % number_below_following:
is_prime = False
break
number_below_following -= 1
if is_prime: yield(following_number)
following_number += 1
if iterations_limiter and iterations_limiter < following_number:
break
def semiprimes():
"""
Generates multiplied prime numbers.
"""
following_number = 2
while 1:
possibilities = []
for a in primes(following_number):
for b in primes(following_number):
if a * b > following_number: possibilities.append(a * b)
following_number = min(possibilities)
yield following_number

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

Traceback (most recent call last):
  File "/tmp/test20120627-11891-1drl9lo", line 2032, in <module>
    PrimesTest.main()
  File "/data/rails/pyfmi-2012/releases/20120501152555/python/homework.py", line 27, in main
    rude.solution = module or imp.load_source('solution', sys.argv[1])
  File "/tmp/solution20120627-11891-70o8np", line 6
    """
      ^
IndentationError: expected an indented block

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

Методи обнови решението на 03.05.2012 15:05 (преди над 8 години)

+from math import sqrt
+
+def primes(iterations_limiter = 0):
+"""
+ Generator for prime numbers.
+"""
+ following_number = 2
+ while 1:
+ number_below_following = round(sqrt(following_number), 0)
+ is_prime = True
+ while number_below_following > 1:
+ if not following_number % number_below_following:
+ is_prime = False
+ break
+ number_below_following -= 1
+ if is_prime: yield(following_number)
+ following_number += 1
+ if iterations_limiter and iterations_limiter < following_number:
+ break
+
+def semiprimes():
+"""
+ Generates multiplied prime numbers.
+"""
+ following_number = 2
+ while 1:
+ possibilities = []
+ for a in primes(following_number):
+ for b in primes(following_number):
+ if a * b > following_number: possibilities.append(a * b)
+ following_number = min(possibilities)
+ yield following_number