Александър обнови решението на 22.04.2012 14:22 (преди над 12 години)
+import math
+
+
+def test_prime(q):
+ index = 2
+ for index in range(3, (q % 2) * (math.ceil(math.sqrt(q)) + 2), 2): #hack in order to skip looping over even
+ if q % index == 0 and q > index:
+ return False
+ return q == 2 or q % 2 == 1 #2 or an odd prime number
+
+
+def primes():
+ p1 = 1
+ while True:
+ p1 = p1 + 2 if p1 > 2 else p1 + 1
+ if test_prime(p1):
+ yield p1
+
+
+def semiprimes():
+ s1 = 4
+ while True:
+ for v1 in range(2, math.ceil(math.sqrt(s1)) + 2, 1):
+ if s1 % v1 == 0 and test_prime(v1) and test_prime(s1 // v1):
+ yield s1
+ break
+ s1 += 1