Мирослав обнови решението на 28.04.2012 13:17 (преди над 12 години)
+def primes():
+ number = 2
+ while True:
+ yield number
+ number = next_prime(number)
+
+def next_prime(number):
+ number += 1
+ while not is_prime(number):
+ number += 1
+ return number
+
+def is_prime(number):
+ middle = int(number/2) + 1
+ for i in range (2, middle):
+ if number % i == 0:
+ return False
+ return True
+
+def semiprimes():
+ number = 4
+ while True:
+ yield number
+ number = next_semiprime(number)
+
+def next_semiprime(number):
+ number += 1
+ while not is_semiprime(number):
+ number += 1
+ return number
+
+def is_semiprime(number):
+ middle = int(number/2) + 1
+ dividers = []
+ for i in range (2, middle):
+ if number % i == 0:
+ dividers.append(i)
+ if number / i == i:
+ dividers.append(i)
+ return (len(dividers) == 2 and
+ is_prime(dividers[0]) and
+ is_prime(dividers[1]))