Елина обнови решението на 03.05.2012 18:57 (преди над 12 години)
+def is_prime(number):
+ if number == 2 or number == 3:
+ return True
+ if number % 2 != 0 and number % 3 != 0:
+ for b in range(1, int((number ** 0.5 + 1)/6 + 1)):
+ if number % (6*b - 1) == 0:
+ return False
+ if number % (6*b + 1) == 0:
+ return False
+ return True
+ return False
+
+
+def primes():
+ yield 2
+ yield 3
+ number = 4
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+
+def is_square_of_prime(n):
+ div = n**0.5
+ div = int(div)
+ if is_prime(div):
+ return div*div == n
+ else:
+ return False
+
+def primes_list(up_to):
+ number = 4
+ list_of_primes = [2,3]
+ for x in range(number, up_to):
+ if is_prime(number):
+ list_of_primes.append(number)
+ number += 1
+ return list_of_primes
+
+
+def semiprimes():
+ number = 4
+ while True:
+ divisors = []
+ if is_square_of_prime(number):
+ yield number
+ else:
+ for divisor in primes_list(number):
+ if number % divisor == 0:
+ divisors.append(divisor)
+ if len(divisors) == 2:
+ if number == divisors[0] * divisors[1]:
+ yield number
+ break
+ number += 1
+
+