Решение на Генератори от Владислав Вълчев

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

Към профила на Владислав Вълчев

Резултати

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

Код

def primes():
# The prime candidate
prime_eventualy = 2
while True:
p = prime_eventualy - 1
#Flag to show if prime_eventualy is a prime
#by deviding it by all integers in the range (1, prime_eventualy)
prime = True
while p != 1:
if not(prime_eventualy % p):
prime = False
break
p -= 1
if prime:
yield prime_eventualy
prime_eventualy += 1
def semiprimes():
'''
This function generates numbers and first checks
if the generated number can be devided by prime
and if the remider of the first division can be devided by prime
'''
semiprime_eventually = 4
while True:
# iterators
pp = primes()
op = primes()
# flag indicating if we have a semiprime number
semi_flag = False
first_test_prime = next(pp)
while semiprime_eventually >= first_test_prime:
#if it can be devided by prime
remider = semiprime_eventually % first_test_prime
if not(remider):
break
first_test_prime = next(pp)
second_test_prime = next(op)
while semiprime_eventually >= second_test_prime:
devided_by_second = not(semiprime_eventually % second_test_prime)
right_primes = (first_test_prime * second_test_prime) == semiprime_eventually
if devided_by_second and right_primes:
semi_flag = True
break;
second_test_prime = next(op)
if semi_flag:
yield semiprime_eventually
semiprime_eventually += 1

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

....
----------------------------------------------------------------------
Ran 4 tests in 1.056s

OK

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

Владислав обнови решението на 25.04.2012 18:44 (преди над 8 години)

+def primes():
+ # The prime candidate
+ prime_eventualy = 2
+ while True:
+ p = prime_eventualy - 1
+ #Flag to show if prime_eventualy is a prime
+ #by deviding it by all integers in the range (1, prime_eventualy)
+ prime = True
+ while p != 1:
+ if not(prime_eventualy % p):
+ prime = False
+ break
+ p -= 1
+
+ if prime:
+ yield prime_eventualy
+ prime_eventualy += 1
+
+
+def semiprimes():
+ '''
+ This function generates numbers and first checks
+ if the generated number can be devided by prime
+ and if the remider of the first division can be devided by prime
+ '''
+ semiprime_eventually = 4
+
+ while True:
+ # iterators
+ pp = primes()
+ op = primes()
+
+ # flag indicating if we have a semiprime number
+ semi_flag = False
+
+ first_test_prime = next(pp)
+
+ while semiprime_eventually >= first_test_prime:
+ #if it can be devided by prime
+ remider = semiprime_eventually % first_test_prime
+ if not(remider):
+ break
+ first_test_prime = next(pp)
+
+ second_test_prime = next(op)
+
+ while semiprime_eventually >= second_test_prime:
+ devided_by_second = not(semiprime_eventually % second_test_prime)
+ right_primes = (first_test_prime * second_test_prime) == semiprime_eventually
+ if devided_by_second and right_primes:
+ semi_flag = True
+ break;
+ second_test_prime = next(op)
+
+ if semi_flag:
+ yield semiprime_eventually
+ semiprime_eventually += 1