# 整数判断是否互质并求逆元的 python 实现

### 算法来自《密码学原理与实践》page129

#!/usr/dev/python
#-*- coding:utf-8 -*-

'''
Extended Euclidean algorithm(a,b)
code by ShaNgZ
algorithm from <cryptography theory and pratice,third editon>
2019_01_09
'''

a = input("a=")
b = input("b=")

s0 = 0
s  = 1
t0 = 0
t  = 1
q  = a/b
r  = a%b
# Ri = Si*R0 + Ti*R1

def euclidean_algorithen():
pass

def extended_euclidean():
global a,b,s0,s,t0,t,q,r
ex_a = a
ex_b = b
ex_s0 = s0
ex_s = 1
ex_t0 = 0
ex_t = 1
ex_q = q
ex_r = r
while(ex_r>0):
temp = ex_t0-ex_q*ex_t
ex_t0 = ex_t
ex_t = temp

temp = ex_s0-ex_q*ex_s
ex_s0 = ex_s
ex_s = temp

ex_a = ex_b
ex_b = ex_r
ex_q = ex_a/ex_b
ex_r = ex_a%ex_b
ex_r = ex_b

print "exr_gcd(%d,%d) = "  % (a,b) + str(ex_r)
if ex_r == 1:
print "inverse(%d) mod %d = %d" % (a,b,ex_s)

#comment: r=gcd(a,b) and sa+tb=r

def multiplicative_inverse():
pass

if __name__ == '__main__':
extended_euclidean()

12-22 7183
06-07 2387
11-28 513
11-30 3736
02-24 538
11-14 1万+
05-05 709
10-28 1万+
04-11 1628
05-12 7734
08-09 1万+
12-20 728
04-06 1067
04-05 4458
11-01 4248

### “相关推荐”对你有帮助么？

•  非常没帮助
•  没帮助
•  一般
•  有帮助
•  非常有帮助 被折叠的  条评论 为什么被折叠? 到【灌水乐园】发言 ShaNgZ1

¥2 ¥4 ¥6 ¥10 ¥20  余额支付 (余额：-- )  扫码支付 获取中  扫码支付 点击重新获取   扫码支付 1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。 余额充值