当前位置: 首页>>代码示例>>Python>>正文


Python ntheory.isprime函数代码示例

本文整理汇总了Python中sympy.ntheory.isprime函数的典型用法代码示例。如果您正苦于以下问题:Python isprime函数的具体用法?Python isprime怎么用?Python isprime使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了isprime函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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


注:本文中的sympy.ntheory.isprime函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。