Решето Аткина
- import math
- def AtkinSieve(limit):
- results = [2, 3, 5]
- sieve = [False] * (limit + 1)
- factor = int(math.sqrt(limit)) + 1
- for i in range(1, factor):
- for j in range(1, factor):
- n = 4 * i ** 2 + j ** 2
- if (n <= limit) and (n % 12 == 1 or n % 12 == 5):
- sieve[n] = not sieve[n]
- n = 3 * i ** 2 + j ** 2
- if (n <= limit) and (n % 12 == 7):
- sieve[n] = not sieve[n]
- if i > j:
- n = 3 * i ** 2 - j ** 2
- if (n <= limit) and (n % 12 == 11):
- sieve[n] = not sieve[n]
- for index in range(5, factor):
- if sieve[index]:
- for jndex in range(index ** 2, limit, index ** 2):
- sieve[jndex] = False
- for index in range(7, limit):
- if sieve[index]:
- results.append(index)
- return results
Пример использования:
- a = AtkinSieve(2000000)
- for i in range(0, len(a)):
- if (a[i] > 1000000) and (a[i] < 2000000):
- print a[i]