跳转至

SageMath常用函数

1.sagemath计算离散对数

x=discrete_log(mod(13,23),mod(2,23))  

#或discrete_log(13,mod(2,23))  

print(x)

2.sagemath求逆元

# a∗b≡1(mod p)

b=inverse_mod(a,p)  

print(a*b%p) #1  

print(b)#求逆元

3.sagemath扩展欧几里得算法

d,u,v=xgcd(20,30)  

print("d:{0} u:{1} v:{2}".format(d,u,v)) #d:10 u:-1 v:1

4.sagemath孙子定理(中国剩余定理)

def chinese_remainder(modulus, remainders):  
  Sum = 0  
  prod = reduce(lambda a, b: a*b, modulus)  
  for m_i, r_i in zip(modulus, remainders):  
    p = prod // m_i  
    Sum += r_i * (inverse_mod(p,m_i)*p)  
  return Sum % prod  

chinese_remainder([3,5,7],[2,3,2]) #23

5.sagemath求欧拉函数

print(euler_phi(60)) #16

原文链接: http://droid10.com/2021/08/13/sagemath常用函数/