def isPrime(N):    
    if N == 1:
        return 0
    if N in sieve:
        return 1
    for i in sieve:
        if (N % i)==0:
            return 0    
    if _fastmath is not None:
        return 1 if _fastmath.isPrime(N) else 0
    N1 = N - 1
    n = 1
    while (n<N):
        n=n<<1
    n = n >> 1  
    for c in sieve[:7]:
        a=int(c) ; d=1 ; t=n
        while (t):  
            x=(d*d) % N
            if x==1 and d!=1 and d!=N1:
                return 0  
            if N1 & t:
                d=(x*a) % N
            else:
                d=x
            t = t >> 1
        if d!=1:
            return 0
    return 1
Пример #2
0
def isPrime(N):
    if N == 1:
        return 0
    if N in sieve:
        return 1
    for i in sieve:
        if (N % i) == 0:
            return 0
    if _fastmath is not None:
        return 1 if _fastmath.isPrime(N) else 0
    N1 = N - 1
    n = 1
    while (n < N):
        n = n << 1
    n = n >> 1
    for c in sieve[:7]:
        a = int(c)
        d = 1
        t = n
        while (t):
            x = (d * d) % N
            if x == 1 and d != 1 and d != N1:
                return 0
            if N1 & t:
                d = (x * a) % N
            else:
                d = x
            t = t >> 1
        if d != 1:
            return 0
    return 1
Пример #3
0
def isPrime(N, randfunc=None):
    """isPrime(N:long, randfunc:callable):bool
    Return true if N is prime.

    If randfunc is omitted, then Random.new().read is used.
    """
    _import_Random()
    if randfunc is None:
        randfunc = Random.new().read

    randint = StrongRandom(randfunc=randfunc).randint

    if N == 1:
        return 0
    if N in sieve:
        return 1
    for i in sieve:
        if (N % i) == 0:
            return 0

    # Use the accelerator if available
    if _fastmath is not None:
        return _fastmath.isPrime(N)

    # Compute the highest bit that's set in N
    N1 = N - 1L
    n = 1L
    while (n < N):
        n = n << 1L
    n = n >> 1L

    # Rabin-Miller test
    for c in sieve[:7]:
        a = long(c)
        d = 1L
        t = n
        while (t):  # Iterate over the bits in N1
            x = (d * d) % N
            if x == 1L and d != 1L and d != N1:
                return 0  # Square root of 1 found
            if N1 & t:
                d = (x * a) % N
            else:
                d = x
            t = t >> 1L
        if d != 1L:
            return 0
Пример #4
0
def isPrime(N, randfunc=None):
    """isPrime(N:long, randfunc:callable):bool
    Return true if N is prime.

    If randfunc is omitted, then Random.new().read is used.
    """
    _import_Random()
    if randfunc is None:
        randfunc = Random.new().read

    randint = StrongRandom(randfunc=randfunc).randint

    if N == 1:
        return 0
    if N in sieve:
        return 1
    for i in sieve:
        if (N % i)==0:
            return 0

    # Use the accelerator if available
    if _fastmath is not None:
        return _fastmath.isPrime(N)

    # Compute the highest bit that's set in N
    N1 = N - 1L
    n = 1L
    while (n<N):
        n=n<<1L
    n = n >> 1L

    # Rabin-Miller test
    for c in sieve[:7]:
        a=long(c) ; d=1L ; t=n
        while (t):  # Iterate over the bits in N1
            x=(d*d) % N
            if x==1L and d!=1L and d!=N1:
                return 0  # Square root of 1 found
            if N1 & t:
                d=(x*a) % N
            else:
                d=x
            t = t >> 1L
        if d!=1L:
            return 0
Пример #5
0
def isPrime(N):
    """isPrime(N:long):bool
    Return true if N is prime.
    """
    if N == 1:
        return 0
    if N in sieve:
        return 1
    for i in sieve:
        if (N % i) == 0:
            return 0

    # Use the accelerator if available
    if _fastmath is not None:
        return _fastmath.isPrime(N)

    # Compute the highest bit that's set in N
    N1 = N - 1
    n = 1
    while n < N:
        n = n << 1
    n = n >> 1

    # Rabin-Miller test
    for c in sieve[:7]:
        a = int(c)
        d = 1
        t = n
        while t:  # Iterate over the bits in N1
            x = (d * d) % N
            if x == 1 and d != 1 and d != N1:
                return 0  # Square root of 1 found
            if N1 & t:
                d = (x * a) % N
            else:
                d = x
            t = t >> 1
        if d != 1:
            return 0
    return 1
Пример #6
0
def isPrime(N):
    """isPrime(N:long):bool
    Return true if N is prime.
    """
    if N == 1:
        return 0
    if N in sieve:
        return 1
    for i in sieve:
        if (N % i) == 0:
            return 0

    # Use the accelerator if available
    if _fastmath is not None:
        return _fastmath.isPrime(N)

    # Compute the highest bit that's set in N
    N1 = N - 1
    n = 1
    while n < N:
        n = n << 1
    n = n >> 1

    # Rabin-Miller test
    for c in sieve[:7]:
        a = int(c)
        d = 1
        t = n
        while t:  # Iterate over the bits in N1
            x = (d * d) % N
            if x == 1 and d != 1 and d != N1:
                return 0  # Square root of 1 found
            if N1 & t:
                d = (x * a) % N
            else:
                d = x
            t = t >> 1
        if d != 1:
            return 0
    return 1