Simple greatest common factor script

fejky qwertzuioplkjhgfdsayxcvbnm at gmail.com
Sun Nov 29 05:56:28 EST 2009


Simple script that calculates greatest common factor using euclid's
theorem.

a = int(input("Enter a: "))
b = int(input("Enter b: "))
m = 1

while True:
    if m != 0:
        if b > a:
            n = b/a
            m = b % a
            print b, " : ", a, " = ", n, " i ost ", m
            b = m
        if a > b:
            n = a/b             # line 13
            m = a % b
            print a, " : ", b, " = ", n, " i ost ", m
            a = m
        if a == b:
            print "NZM(", a, ",", b, ") = ", a
            m = 0
    else: break

but when i run this script:
Enter a: 12345
Enter b: 54321
54321  :  12345  =  4  i ost  4941
12345  :  4941  =  2  i ost  2463
4941  :  2463  =  2  i ost  15
2463  :  15  =  164  i ost  3
15  :  3  =  5  i ost  0
Traceback (most recent call last):
  File "D:\Programing\Python_2.6\math\NZM.py", line 13, in <module>
    n = a/b
ZeroDivisionError: integer division or modulo by zero

I don't see how this script is able to divide by zero. If a and b
switch places everything works ok.

Thanks



More information about the Python-list mailing list