GCD of two numbers in Python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Arshali12
    New Member
    • Aug 2023
    • 4

    GCD of two numbers in Python

    I am trying to write a Python program to find the greatest common divisor of two numbers in Python. I have been following the algorithm described here, but I am having trouble getting it to work.
    Code:
    def gcd(a, b):
        while b != 0:
            a, b = b, a % b
        return a
    When I run this code, I am getting an error:
    Code:
    Traceback (most recent call last):
      File "gcd.py", line 4, in <module>
        return a % b
    ZeroDivisionError: division by zero
    I am not sure what I am doing wrong. Can anyone help me?
    Thanks
Working...