0

我的算法找到两个数字的 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`+'.'

如果您能帮助我找出问题所在,我将不胜感激。

4

3 回答 3

9

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

无论如何,这不是问题。有两个错误。我认为,如果您将打印的中间答案与手工示例进行比较,您应该已经发现了问题。

第一个问题是您在此处的第二行中有错字:

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

在第二行,aqd应该是bqd

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

aqd = aqr
bqd = bqr
aqc = aqd
bqc = bqd

aqd先成为aqr,然后aqc成为aqd。所以aqcaqd结束一样。而您实际上希望按其他顺序分配:

aqc = aqd
bqc = bqd
aqd = aqr
bqd = bqr

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

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

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

希望这可以帮助。

于 2013-06-02T21:01:41.730 回答
2

这是 Bezout 身份的简单版本;给定ab,它返回xyg = gcd( a , b ):

function bezout(a, b)
    if b == 0
        return 1, 0, a
    else
        q, r := divide(a, b)
        x, y, g := bezout(b, r)
        return y, x - q * y, g

divide函数返回商和余数。

于 2013-06-03T00:43:05.860 回答
0

执行您想要的操作的 python 程序(请注意,扩展的 Euclid 算法仅给出一对 Bezout 系数)可能是:

import sys

def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    g, y, x = egcd(b % a, a)
    return (g, x - (b // a) * y, y)

def main():
    if len(sys.argv) != 3:
        's program caluclates LCF, LCM and Bezout identity of two integers
        usage %s a b''' % (sys.argv[0], sys.argv[0])
        sys.exit(1)

    a = int(sys.argv[1])
    b = int(sys.argv[2])

    g, x, y = egcd(a, b)

    print 'HCF =',  g
    print 'LCM =', a*b/g
    print 'Bezout identity:  %i * (%i) + %i * (%i)  = %i' % (a, x, b, y, g)

main()
于 2013-06-03T02:07:19.013 回答