本文整理汇总了Python中sympy.ntheory.isprime函数的典型用法代码示例。如果您正苦于以下问题:Python isprime函数的具体用法?Python isprime怎么用?Python isprime使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isprime函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_isprime
def test_isprime():
s = Sieve()
s.extend(100000)
ps = set(s.primerange(2, 100001))
for n in range(100001):
# if (n in ps) != isprime(n): print n
assert (n in ps) == isprime(n)
assert isprime(179424673)
# Some Mersenne primes
assert isprime(2**61 - 1)
assert isprime(2**89 - 1)
assert isprime(2**607 - 1)
assert not isprime(2**601 - 1)
#Arnault's number
assert isprime(int('''
803837457453639491257079614341942108138837688287558145837488917522297\
427376533365218650233616396004545791504202360320876656996676098728404\
396540823292873879185086916685732826776177102938969773947016708230428\
687109997439976544144845341155872450633409279022275296229414984230688\
1685404326457534018329786111298960644845216191652872597534901'''))
# pseudoprime that passes the base set [2, 3, 7, 61, 24251]
assert not isprime(9188353522314541)
assert _mr_safe_helper(
"if n < 170584961: return mr(n, [350, 3958281543])") == \
' # [350, 3958281543] stot = 1 clear [2, 3, 5, 7, 29, 67, 679067]'
assert _mr_safe_helper(
"if n < 3474749660383: return mr(n, [2, 3, 5, 7, 11, 13])") == \
' # [2, 3, 5, 7, 11, 13] stot = 7 clear == bases'
assert isprime(5.0) is False
开发者ID:Davidjohnwilson,项目名称:sympy,代码行数:30,代码来源:test_ntheory.py
示例2: _number
def _number(expr, assumptions):
# helper method
try:
i = int(expr.round())
if not (expr - i).equals(0):
raise TypeError
except TypeError:
return False
return isprime(i)
开发者ID:EuanFree,项目名称:sympy,代码行数:9,代码来源:ntheory.py
示例3: is_circ
def is_circ(n):
s, i = str(n), 0
if '0' in s or '2' in s or '4' in s or '6' in s or '8' in s: return False
while i < len(s):
if not isprime(int(s)):
return False
s = s[1:len(s)]+s[0]
i+=1
return True
开发者ID:jvujcic,项目名称:ProjectEuler,代码行数:9,代码来源:035.py
示例4: _number_theoretic_transform
def _number_theoretic_transform(seq, prime, inverse=False):
"""Utility function for the Number Theoretic Transform"""
if not iterable(seq):
raise TypeError("Expected a sequence of integer coefficients "
"for Number Theoretic Transform")
p = as_int(prime)
if isprime(p) == False:
raise ValueError("Expected prime modulus for "
"Number Theoretic Transform")
a = [as_int(x) % p for x in seq]
n = len(a)
if n < 1:
return a
b = n.bit_length() - 1
if n&(n - 1):
b += 1
n = 2**b
if (p - 1) % n:
raise ValueError("Expected prime modulus of the form (m*2**k + 1)")
a += [0]*(n - len(a))
for i in range(1, n):
j = int(ibin(i, b, str=True)[::-1], 2)
if i < j:
a[i], a[j] = a[j], a[i]
pr = primitive_root(p)
rt = pow(pr, (p - 1) // n, p)
if inverse:
rt = pow(rt, p - 2, p)
w = [1]*(n // 2)
for i in range(1, n // 2):
w[i] = w[i - 1]*rt % p
h = 2
while h <= n:
hf, ut = h // 2, n // h
for i in range(0, n, h):
for j in range(hf):
u, v = a[i + j], a[i + j + hf]*w[ut * j]
a[i + j], a[i + j + hf] = (u + v) % p, (u - v) % p
h *= 2
if inverse:
rv = pow(n, p - 2, p)
a = [x*rv % p for x in a]
return a
开发者ID:normalhuman,项目名称:sympy,代码行数:56,代码来源:transforms.py
示例5: test_randprime
def test_randprime():
assert randprime(10, 1) is None
assert randprime(2, 3) == 2
assert randprime(1, 3) == 2
assert randprime(3, 5) == 3
raises(ValueError, lambda: randprime(20, 22))
for a in [100, 300, 500, 250000]:
for b in [100, 300, 500, 250000]:
p = randprime(a, a + b)
assert a <= p < (a + b) and isprime(p)
开发者ID:KonstantinTogoi,项目名称:sympy,代码行数:10,代码来源:test_generate.py
示例6: rsa_private_key
def rsa_private_key(p, q, e):
r"""
The RSA *private key* is the pair `(n,d)`, where `n`
is a product of two primes and `d` is the inverse of
`e` (mod `\phi(n)`).
Examples
========
>>> from sympy.crypto.crypto import rsa_private_key
>>> p, q, e = 3, 5, 7
>>> rsa_private_key(p, q, e)
(15, 7)
"""
n = p*q
phi = totient(n)
if isprime(p) and isprime(q) and gcd(e, phi) == 1:
return n, pow(e, phi - 1, phi)
return False
开发者ID:AALEKH,项目名称:sympy,代码行数:20,代码来源:crypto.py
示例7: test_randprime
def test_randprime():
import random
random.seed(1234)
assert randprime(2, 3) == 2
assert randprime(1, 3) == 2
assert randprime(3, 5) == 3
raises(ValueError, lambda: randprime(20, 22))
for a in [100, 300, 500, 250000]:
for b in [100, 300, 500, 250000]:
p = randprime(a, a + b)
assert a <= p < (a + b) and isprime(p)
开发者ID:LuckyStrikes1090,项目名称:sympy,代码行数:11,代码来源:test_ntheory.py
示例8: _number
def _number(expr, assumptions):
# helper method
exact = not expr.atoms(Float)
try:
i = int(expr.round())
if (expr - i).equals(0) is False:
raise TypeError
except TypeError:
return False
if exact:
return isprime(i)
开发者ID:asmeurer,项目名称:sympy,代码行数:11,代码来源:ntheory.py
示例9: sumcons
def sumcons(Nmin, Nmax):
retval = (-1, -1)
tot = 0
for i, n in enumerate(nt.primerange(Nmin, Nmax)):
if i == 0:
continue
if tot >= Nmax:
break
if nt.isprime(tot):
retval = (i, tot)
tot = tot + n
return retval
开发者ID:evan-phelps,项目名称:project-euler,代码行数:12,代码来源:p50.py
示例10: test_frt
def test_frt():
SIZE = 59
try:
import sympy.ntheory as sn
assert sn.isprime(SIZE) == True
except ImportError:
pass
# Generate a test image
L = np.tri(SIZE, dtype=np.int32) + np.tri(SIZE, dtype=np.int32)[::-1]
f = frt2(L)
fi = ifrt2(f)
assert len(np.nonzero(L - fi)[0]) == 0
开发者ID:ChrisBeaumont,项目名称:scikit-image,代码行数:13,代码来源:test_finite_radon_transform.py
示例11: rsa_public_key
def rsa_public_key(p, q, e):
r"""
The RSA *public key* is the pair `(n,e)`, where `n`
is a product of two primes and `e` is relatively
prime (coprime) to the Euler totient `\phi(n)`.
Examples
========
>>> from sympy.crypto.crypto import rsa_public_key
>>> p, q, e = 3, 5, 7
>>> n, e = rsa_public_key(p, q, e)
>>> n
15
>>> e
7
"""
n = p*q
phi = totient(n)
if isprime(p) and isprime(q) and gcd(e, phi) == 1:
return n, e
return False
开发者ID:Upabjojr,项目名称:sympy,代码行数:23,代码来源:crypto.py
示例12: isItJamCoin
def isItJamCoin(number):
binnumber = bin(number)[2:]
divisors = []
for i in range(2, 11):
current = int(binnumber, i)
if isprime(current):
return None
divisor = min(factorint(current).keys())
if divisor == current:
return None
divisors.append(divisor)
return divisors
开发者ID:Neverous,项目名称:individual,代码行数:15,代码来源:c.py
示例13: GetLongestConsSumPrimes
def GetLongestConsSumPrimes(step,limit):
sum = step
count = 1
i = nextprime(step)
while (sum + i)<limit:
sum += i
count += 1
i = nextprime(i)
while isprime(sum) == False :
i = prevprime(i)
sum -= i
count -= 1
return (count,sum)
开发者ID:azusa0127,项目名称:ProjectEuler,代码行数:15,代码来源:p50.py
示例14: test_isprime
def test_isprime():
s = Sieve()
s.extend(100000)
ps = set(s.primerange(2, 100001))
for n in range(100001):
assert (n in ps) == isprime(n)
assert isprime(179424673)
# Some Mersenne primes
assert isprime(2**61 - 1)
assert isprime(2**89 - 1)
assert isprime(2**607 - 1)
assert not isprime(2**601 - 1)
开发者ID:smichr,项目名称:sympy-live,代码行数:12,代码来源:test_ntheory.py
示例15: _check_cycles_alt_sym
def _check_cycles_alt_sym(perm):
"""
Checks for cycles of prime length p with n/2 < p < n-2.
Here `n` is the degree of the permutation. This is a helper function for
the function is_alt_sym from sympy.combinatorics.perm_groups.
Examples
========
>>> from sympy.combinatorics.util import _check_cycles_alt_sym
>>> from sympy.combinatorics.permutations import Permutation
>>> a = Permutation([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [11, 12]])
>>> _check_cycles_alt_sym(a)
False
>>> b = Permutation([[0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10]])
>>> _check_cycles_alt_sym(b)
True
See Also
========
sympy.combinatorics.perm_groups.PermutationGroup.is_alt_sym
"""
n = perm.size
af = perm.array_form
current_len = 0
total_len = 0
used = set()
for i in range(n // 2):
if not i in used and i < n // 2 - total_len:
current_len = 1
used.add(i)
j = i
while (af[j] != i):
current_len += 1
j = af[j]
used.add(j)
total_len += current_len
if current_len > n // 2 and current_len < n - 2 and isprime(
current_len):
return True
return False
开发者ID:tokencolour,项目名称:sympy,代码行数:44,代码来源:util.py
示例16: _inv_totient_estimate
def _inv_totient_estimate(m):
"""
Find ``(L, U)`` such that ``L <= phi^-1(m) <= U``.
Examples
========
>>> from sympy.polys.polyroots import _inv_totient_estimate
>>> _inv_totient_estimate(192)
(192, 840)
>>> _inv_totient_estimate(400)
(400, 1750)
"""
primes = [ d + 1 for d in divisors(m) if isprime(d + 1) ]
a, b = 1, 1
for p in primes:
a *= p
b *= p - 1
L = m
U = int(math.ceil(m*(float(a)/b)))
P = p = 2
primes = []
while P <= U:
p = nextprime(p)
primes.append(p)
P *= p
P //= p
b = 1
for p in primes[:-1]:
b *= p - 1
U = int(math.ceil(m*(float(P)/b)))
return L, U
开发者ID:bjodah,项目名称:sympy,代码行数:43,代码来源:polyroots.py
示例17: test_isprime
def test_isprime():
s = Sieve()
s.extend(100000)
ps = set(s.primerange(2, 100001))
for n in range(100001):
assert (n in ps) == isprime(n)
assert isprime(179424673)
# Some Mersenne primes
assert isprime(2**61 - 1)
assert isprime(2**89 - 1)
assert isprime(2**607 - 1)
assert not isprime(2**601 - 1)
#Arnault's number
assert isprime(int('''
803837457453639491257079614341942108138837688287558145837488917522297\
427376533365218650233616396004545791504202360320876656996676098728404\
396540823292873879185086916685732826776177102938969773947016708230428\
687109997439976544144845341155872450633409279022275296229414984230688\
1685404326457534018329786111298960644845216191652872597534901'''))
# pseudoprime that passes the base set [2, 3, 7, 61, 24251]
assert not isprime(9188353522314541)
开发者ID:Praveen-Ramanujam,项目名称:MobRAVE,代码行数:21,代码来源:test_ntheory.py
示例18: test_elgamal_private_key
def test_elgamal_private_key():
a, b, _ = elgamal_private_key(digit=100)
assert isprime(a)
assert is_primitive_root(b, a)
assert len(bin(a)) >= 102
开发者ID:AdrianPotter,项目名称:sympy,代码行数:5,代码来源:test_crypto.py
示例19: test_dh_private_key
def test_dh_private_key():
p, g, _ = dh_private_key(digit = 100)
assert isprime(p)
assert is_primitive_root(g, p)
assert len(bin(p)) >= 102
开发者ID:asmeurer,项目名称:sympy,代码行数:5,代码来源:test_crypto.py
示例20: zzx_zassenhaus
def zzx_zassenhaus(f):
"""Factor square-free polynomials over Z[x]. """
n = zzx_degree(f)
if n == 1:
return [f]
A = zzx_max_norm(f)
b = zzx_LC(f)
B = abs(int(sqrt(n+1)*2**n*A*b))
C = (n+1)**(2*n)*A**(2*n-1)
gamma = int(ceil(2*log(C, 2)))
prime_max = int(2*gamma*log(gamma))
for p in xrange(3, prime_max+1):
if not isprime(p) or b % p == 0:
continue
F = gf_from_int_poly(f, p)
if gf_sqf_p(F, p):
break
l = int(ceil(log(2*B + 1, p)))
modular = []
for ff in gf_factor_sqf(F, p)[1]:
modular.append(gf_to_int_poly(ff, p))
g = zzx_hensel_lift(p, f, modular, l)
T = set(range(len(g)))
factors, s = [], 1
while 2*s <= len(T):
for S in subsets(T, s):
G, H = [b], [b]
S = set(S)
for i in S:
G = zzx_mul(G, g[i])
for i in T-S:
H = zzx_mul(H, g[i])
G = zzx_trunc(G, p**l)
H = zzx_trunc(H, p**l)
G_norm = zzx_l1_norm(G)
H_norm = zzx_l1_norm(H)
if G_norm*H_norm <= B:
T = T - S
G = zzx_primitive(G)[1]
f = zzx_primitive(H)[1]
factors.append(G)
b = zzx_LC(f)
break
else:
s += 1
return factors + [f]
开发者ID:jcockayne,项目名称:sympy-rkern,代码行数:66,代码来源:integerpolys.py
注:本文中的sympy.ntheory.isprime函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论