本文整理汇总了Python中sympy.ntheory.sieve.primerange函数的典型用法代码示例。如果您正苦于以下问题:Python primerange函数的具体用法?Python primerange怎么用?Python primerange使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了primerange函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _swing
def _swing(cls, n):
if n < 33:
return cls._small_swing[n]
else:
N, primes = int(_sqrt(n)), []
for prime in sieve.primerange(3, N + 1):
p, q = 1, n
while True:
q //= prime
if q > 0:
if q & 1 == 1:
p *= prime
else:
break
if p > 1:
primes.append(p)
for prime in sieve.primerange(N + 1, n//3 + 1):
if (n // prime) & 1 == 1:
primes.append(prime)
L_product = R_product = 1
for prime in sieve.primerange(n//2 + 1, n + 1):
L_product *= prime
for prime in primes:
R_product *= prime
return L_product*R_product
示例2: _facmod
def _facmod(self, n, q):
res, N = 1, int(_sqrt(n))
# Exponent of prime p in n! is e_p(n) = [n/p] + [n/p**2] + ...
# for p > sqrt(n), e_p(n) < sqrt(n), the primes with [n/p] = m,
# occur consecutively and are grouped together in pw[m] for
# simultaneous exponentiation at a later stage
pw = [1]*N
m = 2 # to initialize the if condition below
for prime in sieve.primerange(2, n + 1):
if m > 1:
m, y = 0, n // prime
while y:
m += y
y //= prime
if m < N:
pw[m] = pw[m]*prime % q
else:
res = res*pow(prime, m, q) % q
for ex, bs in enumerate(pw):
if ex == 0 or bs == 1:
continue
if bs == 0:
return 0
res = res*pow(bs, ex, q) % q
return res
示例3: eval
def eval(cls, n, k):
n, k = map(sympify, (n, k))
if k.is_Number:
if k.is_Integer:
if k < 0:
return S.Zero
elif k == 0 or n == k:
return S.One
elif n.is_Integer and n >= 0:
n, k = int(n), int(k)
if k > n:
return S.Zero
elif k > n // 2:
k = n - k
M, result = int(_sqrt(n)), 1
for prime in sieve.primerange(2, n+1):
if prime > n - k:
result *= prime
elif prime > n // 2:
continue
elif prime > M:
if n % prime < k % prime:
result *= prime
else:
N, K = n, k
exp = a = 0
while N > 0:
a = int((N % prime) < (K % prime + a))
N, K = N // prime, K // prime
exp = a + exp
if exp > 0:
result *= prime**exp
return C.Integer(result)
else:
result = n - k + 1
for i in xrange(2, k+1):
result *= n-k+i
result /= i
return result
elif k.is_negative:
return S.Zero
elif (n - k).simplify().is_negative:
return S.Zero
else:
d = n - k
if d.is_Integer:
return cls.eval(n, d)
示例4: eval
def eval(cls, r, k):
r, k = map(sympify, (r, k))
if k.is_Number:
if k is S.Zero:
return S.One
elif k.is_Integer:
if k.is_negative:
return S.Zero
else:
if r.is_Integer and r.is_nonnegative:
r, k = int(r), int(k)
if k > r:
return S.Zero
elif k > r // 2:
k = r - k
M, result = int(sqrt(r)), 1
for prime in sieve.primerange(2, r+1):
if prime > r - k:
result *= prime
elif prime > r // 2:
continue
elif prime > M:
if r % prime < k % prime:
result *= prime
else:
R, K = r, k
exp = a = 0
while R > 0:
a = int((R % prime) < (K % prime + a))
R, K = R // prime, K // prime
exp = a + exp
if exp > 0:
result *= prime**exp
return C.Integer(result)
else:
result = r - k + 1
for i in xrange(2, k+1):
result *= r-k+i
result /= i
return result
if k.is_integer:
if k.is_negative:
return S.Zero
else:
return C.gamma(r+1)/(C.gamma(r-k+1)*C.gamma(k+1))
示例5: _eval_apply
def _eval_apply(cls, r, k):
r, k = map(Basic.sympify, (r, k))
if isinstance(k, Basic.Number):
if isinstance(k, Basic.Zero):
return S.One
elif isinstance(k, Basic.Integer):
if k.is_negative:
return S.Zero
else:
if isinstance(r, Basic.Integer) and r.is_nonnegative:
r, k = int(r), int(k)
if k > r:
return S.Zero
elif k > r / 2:
k = r - k
M, result = int(sqrt(r)), 1
for prime in sieve.primerange(2, r+1):
if prime > r - k:
result *= prime
elif prime > r / 2:
continue
elif prime > M:
if r % prime < k % prime:
result *= prime
else:
R, K = r, k
exp = a = 0
while R > 0:
a = int((R % prime) < (K % prime + a))
R, K = R / prime, K / prime
exp = a + exp
if exp > 0:
result *= prime**exp
return Basic.Integer(result)
else:
result = r - k + 1
for i in xrange(2, k+1):
result *= r-k+i
result /= i
return result
if k.is_integer:
if k.is_negative:
return S.Zero
else:
return Basic.gamma(r+1)/(Basic.gamma(r-k+1)*Basic.gamma(k+1))
示例6: _eval
def _eval(self, n, k):
# n.is_Number and k.is_Integer and k != 1 and n != k
from sympy.functions.elementary.exponential import log
from sympy.core import N
if k.is_Integer:
if n.is_Integer and n >= 0:
n, k = int(n), int(k)
if k > n:
return S.Zero
elif k > n // 2:
k = n - k
if HAS_GMPY:
from sympy.core.compatibility import gmpy
return Integer(gmpy.bincoef(n, k))
prime_count_estimate = N(n / log(n))
# if the number of primes less than n is less than k, use prime sieve method
# otherwise it is more memory efficient to compute factorials explicitly
if prime_count_estimate < k:
M, result = int(_sqrt(n)), 1
for prime in sieve.primerange(2, n + 1):
if prime > n - k:
result *= prime
elif prime > n // 2:
continue
elif prime > M:
if n % prime < k % prime:
result *= prime
else:
N, K = n, k
exp = a = 0
while N > 0:
a = int((N % prime) < (K % prime + a))
N, K = N // prime, K // prime
exp = a + exp
if exp > 0:
result *= prime**exp
else:
result = ff(n, k) / factorial(k)
return Integer(result)
else:
d = result = n - k + 1
for i in range(2, k + 1):
d += 1
result *= d
result /= i
return result
示例7: _eval
def _eval(self, n, k):
# n.is_Number and k.is_Integer and k != 1 and n != k
if k.is_Integer:
if n.is_Integer and n >= 0:
n, k = int(n), int(k)
if k > n:
return S.Zero
elif k > n // 2:
k = n - k
M, result = int(_sqrt(n)), 1
for prime in sieve.primerange(2, n + 1):
if prime > n - k:
result *= prime
elif prime > n // 2:
continue
elif prime > M:
if n % prime < k % prime:
result *= prime
else:
N, K = n, k
exp = a = 0
while N > 0:
a = int((N % prime) < (K % prime + a))
N, K = N // prime, K // prime
exp = a + exp
if exp > 0:
result *= prime**exp
return Integer(result)
else:
d = result = n - k + 1
for i in range(2, k + 1):
d += 1
result *= d
result /= i
return result
示例8: _eval_Mod
def _eval_Mod(self, q):
n, k = self.args
if not all(x.is_integer for x in (n, k, q)):
raise ValueError("Integers expected for binomial Mod")
if all(x.is_Integer for x in (n, k, q)):
n, k = map(int, (n, k))
aq, res = abs(q), 1
# handle negative integers k or n
if k < 0:
return 0
if n < 0:
n = -n + k - 1
res = -1 if k%2 else 1
# non negative integers k and n
if k > n:
return 0
isprime = aq.is_prime
aq = int(aq)
if isprime:
if aq < n:
# use Lucas Theorem
N, K = n, k
while N or K:
res = res*binomial(N % aq, K % aq) % aq
N, K = N // aq, K // aq
else:
# use Factorial Modulo
d = n - k
if k > d:
k, d = d, k
kf = 1
for i in range(2, k + 1):
kf = kf*i % aq
df = kf
for i in range(k + 1, d + 1):
df = df*i % aq
res *= df
for i in range(d + 1, n + 1):
res = res*i % aq
res *= pow(kf*df % aq, aq - 2, aq)
res %= aq
else:
# Binomial Factorization is performed by calculating the
# exponents of primes <= n in `n! /(k! (n - k)!)`,
# for non-negative integers n and k. As the exponent of
# prime in n! is e_p(n) = [n/p] + [n/p**2] + ...
# the exponent of prime in binomial(n, k) would be
# e_p(n) - e_p(k) - e_p(n - k)
M = int(_sqrt(n))
for prime in sieve.primerange(2, n + 1):
if prime > n - k:
res = res*prime % aq
elif prime > n // 2:
continue
elif prime > M:
if n % prime < k % prime:
res = res*prime % aq
else:
N, K = n, k
exp = a = 0
while N > 0:
a = int((N % prime) < (K % prime + a))
N, K = N // prime, K // prime
exp += a
if exp > 0:
res *= pow(prime, exp, aq)
res %= aq
return Integer(res % q)