Velocity Reviews > Simple greatest common factor script

# Simple greatest common factor script

fejky
Guest
Posts: n/a

 11-29-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

Patrick Sabin
Guest
Posts: n/a

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

Have a look at your if-statements. It is possible, that both your if's
are executed in one loop iteration (you can check this using pdb). You
may want to try elif instead.

- Patrick

fejky
Guest
Posts: n/a

 11-29-2009
I have no idea how i missed that.

Thanks!