Solovar-Stassen素性检验

import random
import time
import math
#计算r
def r_value(n,b):
    r0 = int((n-1)/2)
    print("指数:%d"%r0)
    r1 = int(b**r0)
    #print("{0}^{1}={2}".format(b,r0,r1))####  n太大时注释掉
    r = int(r1%n)
    print("{0}^{1} mod {2} = {3}".format(b,r0,n,r))#####
    if r == n-1:
        r = -1
    print("r=%d"%r)####
    print(".................................")
    return r

#计算勒让得符号
def lerangde(n,b):
    if b%n == 0:
        s = 0
        print("s={0}".format(s))            
    #二次互反律
    else:    
        lerangde = []
        while True:       
            if b < n :
                if b%2 == 0 and b != 2:
                    f = 0
                    while True:
                        if b%2 == 0:
                            f += 1 #
                            b = int(b/2)
                        else:
                            break
                    m0 = f
                    m3 = int(n**2-1)
                    m2 = int(m3/8)
                    m1 = int((-1)**m2)
                    print("项数:{0} 底数:{1}".format(m0,m1))
                    if m0%2 == 0 or m1 == 1:
                        s0 = 1
                        lerangde.append(s0)      
                    else:
                        s0 = -1
                        lerangde.append(s0)
                if b == 1:
                    s = 1
                    print("s(b=1)={0}".format(s))
                    print("[ ",end="")
                    for u in lerangde:
                        
                        print(" %d "%u,end="")
                        s *= u
                    print("]")
                    s = int(s)    
                    print("s={0}".format(s))    
                    break  
                if  b == 2:
                    s3 = int(n**2-1)
                    s1 = int(s3/8)
                    s = int(-1)**s1    
                    print("s(b=2)={0}".format(s))
                    print("[ ",end="")
                    for u in lerangde:
                        print(" %d "%u,end="")
                        s *= u
                    print("]")
                    print("s={0}".format(s))
                    break

                h = int(((n-1)/2)*((b-1)/2))
                print("h={0}".format(h))
                s0 = int((-1)**h)
                print("s0={0}".format(s0))
                lerangde.append(s0)
                tmp = b
                b = n
                n = tmp  
            if b > n:
                b = int(b%n)   
            if b == 1:
                s = 1
                print("s(b=1)={0}".format(s))
                print("[ ",end="")
                for u in lerangde:
                    
                    print(" %d "%u,end="")
                    s *= u
                print("]")
                s = int(s)    
                print("s={0}".format(s))    
                break  
            if  b == 2:
                s3 = int(n**2-1)
                s1 = int(s3/8)
                s = int(-1)**s1    
                print("s(b=2)={0}".format(s))
                print("[ ",end="")
                for u in lerangde:
                    print(" %d "%u,end="")
                    s *= u
                print("]")
                print("s={0}".format(s))
                break
    return s            


def yinshu(n):
    yinshu2 = [] #创建一个列表,分解质因数,将雅可比符号转化为勒让得符号
    print("%d的因数是:["%n,end="")
    for i in range(2,n):
        while True:
            if n%i == 0:
                yinshu2.append(i)
                print(" %d "%i,end="")
                n = n/i
            else:
                break
    print("]")
    return yinshu2

def panduan(n):
    for i in range(2,n):
        while True:
            if n%i == 0:
                return 1   
            else:
                break
    return 0
def jacobi(n,b):
    
    s = 1
    for u in yinshu(n):
        
        s *= lerangde(u,b)
    
    return s      
                

                

#主函数
while True:
    
    n = int(input("请输入一个奇整数n:"))

    while True:
            key_b = input("随机产生基数b:b || 自定义基数b:B || 重新输入n:n   || 退出:CHI  请输入:")
            
            if key_b == 'b' :
                t = int(input("请输入安全参数t:"))
                time1_start = time.time() #计算初始时间
                k =1
                while k <= t:
                    b = random.randint(2,n-1) 
                    print("随机数:b=%d"%b)
                    r = r_value(n,b)
                    if r != 1 and r!= -1:
                        print("...............")
                        print("***%d是合数***"%n)
                        break
                    else:
                        if panduan(n) == 1:
                            s = jacobi(n,b)
                            
                        else:
                            s = lerangde(n,b)
                        print("关键r:=======%d======"%r)
                        print('关键s:=======%d======'%s)
                        if r != s:
                            print("...............")
                            print("****%d是合数****"%n)
                            break
                        else:
                            print("...............")
                            if panduan(n) == 1:
                                print("****{0}是基于{1}伪素数*****".format(n,b))
                            else:
                                print("****%d是素数*****"%n)
                    print("=================={%d}=========================="%k)
                    k += 1
                time1_end = time.time()#计算检验一次结束时间
                print("Solovar_Stassen素性检验一次时间:{0}".format(time1_end - time1_start))
                
            elif key_b == 'B':
                b = int(input("请输入自定义的b:"))
                time2_start = time.time() #计算初始时间
                print("随机数:b=%d"%b)
                r = r_value(n,b)
                if r != 1 and r!= -1:
                    print("...............")
                    print("***%d是合数***"%n)
                    break
                else:
                    if panduan(n) == 1:
                        s = jacobi(n,b)
                    else:
                        s = lerangde(n,b)
                    print("关键r:=======%d======"%r)
                    print('关键s:=======%d======'%s)
                    if r != s:
                        print("...............")
                        print("*****%d是合数***"%n)
                        break
                    else:
                        print("...............")
                        if panduan(n) == 1:
                            print("****{0}是基于{1}伪素数*****".format(n,b))
                        else:
                            print("****%d是素数*****"%n)
                time2_end = time.time()#计算检验一次结束时间
                print("Solovar_Stassen素性检验一次时间:{0}".format(time2_end - time2_start))
            elif key_b == "n":
                print("-------------------------------------------")
                break    
            elif key_b == "CHI":
                exit(0)
            else:
                print("输入错误,请重新输入============")
            
            
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值