Simple greatest common factor script

F

fejky

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
 
P

Patrick Sabin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top