Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simple greatest common factor script

1 view
Skip to first unread message

fejky

unread,
Nov 29, 2009, 5:56:28 AM11/29/09
to
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

unread,
Nov 29, 2009, 6:18:51 AM11/29/09
to fejky, pytho...@python.org
> 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

unread,
Nov 29, 2009, 6:23:25 AM11/29/09
to
I have no idea how i missed that.

Thanks!

0 new messages