我的扩展欧几里得算法(python)出了什么问题?

0 python algorithm math

我找到两个数字的HCF的算法,在表格中显示的理由r = a*aqr + b*bqr,只是部分有效,即使我很确定我已经输入了所有正确的公式 - 基本上,它可以并且会找到HCF,但我我也试图提供Bezout引理的演示,所以我需要显示上述显示的理由.该程序:

# twonumbers.py
inp = 0
a = 0
b = 0
mul = 0
s = 1
r = 1
q = 0
res = 0
aqc = 1
bqc = 0
aqd = 0
bqd = 1
aqr = 0
bqr = 0
res = 0
temp = 0
fin_hcf = 0
fin_lcd = 0
seq = []
inp = input('Please enter the first number, "a":\n')
a = inp
inp = input('Please enter the second number, "b":\n')
b = inp
mul = a * b # Will come in handy later!
if a < b:
    print 'As you have entered the first number as smaller than the second, the program will swap a and b before proceeding.'
    temp = a
    a = b
    b = temp
else:
    print 'As the inputted value a is larger than or equal to b, the program has not swapped the values a and b.'
print 'Thank you. The program will now compute the HCF and simultaneously demonstrate Bezout\'s Lemma.'
print `a`+' = ('+`aqc`+' x '+`a`+') + ('+`bqc`+' x '+`b`+').'
print `b`+' = ('+`aqd`+' x '+`a`+') + ('+`bqd`+' x '+`b`+').'
seq.append(a)
seq.append(b)
c = a
d = b
while r != 0:
    if s != 1:
        c = seq[s-1]
        d = seq[s]
    res = divmod(c,d)
    q = res[0]
    r = res[1]
    aqr = aqc - (q * aqd)#These two lines are the main part of the justification
    bqr = bqc - (q * aqd)#-/
    print `r`+' = ('+`aqr`+' x '+`a`+') + ('+`bqr`+' x '+`b`+').'
    aqd = aqr
    bqd = bqr
    aqc = aqd
    bqc = bqd
    s = s + 1
    seq.append(r)
fin_hcf = seq[-2] # Finally, the HCF.
fin_lcd = mul / fin_hcf
print 'Using Euclid\'s Algorithm, we have now found the HCF of '+`a`+' and '+`b`+': it is '+`fin_hcf`+'.'
print 'We can now also find the LCD (LCM) of '+`a`+' and '+`b`+' using the following method:'
print `a`+' x '+`b`+' = '+`mul`+';'
print `mul`+' / '+`fin_hcf`+' (the HCF) = '+`fin_lcd`+'.'
print 'So, to conclude, the HCF of '+`a`+' and '+`b`+' is '+`fin_hcf`+' and the LCD (LCM) of '+`a`+' and '+`b`+' is '+`fin_lcd`+'.'
Run Code Online (Sandbox Code Playgroud)

如果你能帮助我找出这个问题,我将不胜感激.

str*_*bly 8

嗯,你的程序相当冗长,因此难以阅读.例如,您不需要在前几行中初始化大量这些变量.并且不需要分配给inp变量然后将其复制到a然后b.并且您根本不使用seq列表或s变量.

无论如何,这不是问题.有两个错误.我认为,如果你把印刷的中间答案与手工制作的例子进行比较,你应该找到问题所在.

第一个问题是你在第二行有一个拼写错误:

aqr = aqc - (q * aqd)#These two lines are the main part of the justification
bqr = bqc - (q * aqd)#-/
Run Code Online (Sandbox Code Playgroud)

在第二行,aqd应该是bqd

第二个问题是在这段代码中

aqd = aqr
bqd = bqr
aqc = aqd
bqc = bqd
Run Code Online (Sandbox Code Playgroud)

你做aqdaqr,然后aqcaqd.因此,aqcaqd最终相同.而您实际上想要以其他顺序进行分配:

aqc = aqd
bqc = bqd
aqd = aqr
bqd = bqr
Run Code Online (Sandbox Code Playgroud)

然后代码工作.但我更愿意看到它写得更像这样,我认为更清楚.我遗漏了打印件,但我相信你可以把它们加回来:

a = input('Please enter the first number, "a":\n')
b = input('Please enter the second number, "b":\n')
if a < b:
    a,b = b,a

r1,r2 = a,b
s1,s2 = 1,0
t1,t2 = 0,1
while r2 > 0:
    q,r = divmod(r1,r2)
    r1,r2 = r2,r
    s1,s2 = s2,s1 - q * s2
    t1,t2 = t2,t1 - q * t2

print r1,s1,t1
Run Code Online (Sandbox Code Playgroud)

最后,我认为可能值得看一个表达解决方案结构的递归版本.

希望这可以帮助.