57 lines
1.1 KiB
Python
57 lines
1.1 KiB
Python
|
|
def gcd(x, y):
|
|
if 0 == y:
|
|
return x
|
|
else:
|
|
return gcd(y, x % y)
|
|
|
|
|
|
def lcm(x, y):
|
|
return x * y // gcd(x, y)
|
|
|
|
|
|
def pow_mod(base, exp, mod):
|
|
result = 1
|
|
for digit in bin(exp)[2:]:
|
|
result = result ** 2 % mod
|
|
if '1' == digit:
|
|
result = result * base % mod
|
|
return result
|
|
|
|
|
|
def make_prime(limit):
|
|
if limit < 5:
|
|
if limit < 2: return
|
|
yield 2
|
|
if limit < 3: return
|
|
yield 3
|
|
return
|
|
|
|
n = (limit + 1) // 6
|
|
|
|
a = [True] * n
|
|
b = [True] * n
|
|
|
|
for i in range((int(limit ** 0.5) + 1) // 6 + 1):
|
|
if a[i]:
|
|
p = 6 * i + 7
|
|
f = 7 * i + 7
|
|
g = 5 * i + 5
|
|
a[f::p] = [False] * ((n - f - 1) // p + 1)
|
|
b[g::p] = [False] * ((n - g - 1) // p + 1)
|
|
if b[i]:
|
|
p = 6 * i + 5
|
|
f = 5 * i + 3
|
|
g = 7 * i + 5
|
|
a[f::p] = [False] * ((n - f - 1) // p + 1)
|
|
b[g::p] = [False] * ((n - g - 1) // p + 1)
|
|
|
|
yield 2
|
|
yield 3
|
|
for i in range(n):
|
|
if b[i]:
|
|
yield 6 * i + 5
|
|
if a[i]:
|
|
yield 6 * i + 7
|
|
|