无法改变米勒rabin公式来计算误差?

时间:2016-11-24 02:25:24

标签: python algorithm refactoring syntax-error probability

我如何确定不是素数的数字,以及我的等式表示它是素数?

让我们说100-1000个数字是素数,并且根据维基百科的定义,“检测所有复合材料(再一次,这意味着:对于每个复合数n,至少3/4(Miller-Rabin)或1 /数字a的2(Solovay-Strassen)是n)的复合性的见证。“

我如何更改误差估计的公式?

from random import randrange

def miller-rabin(i, k):
    """
    Miller Rabin written in Python
    :param i: i is the number to check for primality
    :param k: is the security value
    :return: True is the number is prime, false if not
    """
    #base case 1
    if i < 2 or i % 2 == 0:
        return False

    #case 2
    if i == 2:
        return True

    r = 0
    s = i - 1
    #create initial equation n-1 = 2^(u*r)
    while s % 2 == 0:
        r += 1
        s //= 2
    for _ in range(k):
        a = randrange(2, i-1)
        x = pow(a, s, i)
        if x == 1 or x == i - 1:
            continue
        for _ in range( r - 1):
            x = pow(x, 2, i)
            if x == i - 1:
                break
        else:
            return False
    return True

print(millerR(10, 5))

0 个答案:

没有答案