Ивелин обнови решението на 01.05.2012 13:50 (преди над 12 години)
+def primes():
+ base = 3
+ yield 2
+ yield base
+ found_primes = [2, 3]
+ while 1:
+ check = True
+ base += 2
+ base_root = base ** 0.5
+ for prime in found_primes:
+ if prime > base_root: break
+ if base % prime == 0:
+ check = False
+ break
+ if check:
+ found_primes.append(base)
+ yield base
+
+def semiprimes():
+ base = 3
+ found_primes = [2, 3]
+ while 1:
+ is_prime = True
+ is_semiprime = False
+ base += 1
+ for prime in found_primes:
+ if base % prime == 0:
+ is_prime = False
+ if base / prime in found_primes:
+ is_semiprime = True
+ break
+
+ if is_prime:
+ found_primes.append(base)
+ if is_semiprime:
+ yield base